Merge "Error code support for ErrorMessage"
[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.MultipartRequestInput;
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>> multipartRequest(MultipartRequestInput input) {
185         return sendToSwitchFuture(input, "multi-part-request sending failed");
186     }
187
188     @Override
189     public Future<RpcResult<Void>> portMod(PortModInput input) {
190         return sendToSwitchFuture(input, "port-mod-input sending failed");
191     }
192
193     @Override
194     public Future<RpcResult<RoleRequestOutput>> roleRequest(
195             RoleRequestInput input) {
196         return sendToSwitchExpectRpcResultFuture(
197                 input, RoleRequestOutput.class, "role-request-config-input sending failed");
198     }
199
200     @Override
201     public Future<RpcResult<Void>> setConfig(SetConfigInput input) {
202         return sendToSwitchFuture(input, "set-config-input sending failed");
203     }
204
205     @Override
206     public Future<RpcResult<Void>> tableMod(TableModInput input) {
207         return sendToSwitchFuture(input, "table-mod-input sending failed");
208     }
209
210     @Override
211     public Future<RpcResult<GetAsyncOutput>> getAsync(GetAsyncInput input) {
212         return sendToSwitchExpectRpcResultFuture(
213                 input, GetAsyncOutput.class, "get-async-input sending failed");
214     }
215
216     @Override
217     public Future<RpcResult<Void>> setAsync(SetAsyncInput input) {
218         return sendToSwitchFuture(input, "set-async-input sending failed");
219     }
220
221     @Override
222     public Future<Boolean> disconnect() {
223         ChannelFuture disconnectResult = channel.disconnect();
224         responseCache.invalidateAll();
225         disconnectOccured = true;
226
227         String failureInfo = "switch disconnecting failed";
228         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
229         String message = "Check the switch connection";
230         return handleTransportChannelFuture(disconnectResult, failureInfo, errorSeverity, message);
231     }
232
233     @Override
234     public boolean isAlive() {
235         return channel.isOpen();
236     }
237
238     @Override
239     public void setMessageListener(OpenflowProtocolListener messageListener) {
240         this.messageListener = messageListener;
241     }
242
243     @Override
244     public void consume(DataObject message) {
245         LOG.debug("ConsumeIntern msg");
246         if (disconnectOccured ) {
247             return;
248         }
249         if (message instanceof Notification) {
250             // System events
251             if (message instanceof DisconnectEvent) {
252                 systemListener.onDisconnectEvent((DisconnectEvent) message);
253                 responseCache.invalidateAll();
254                 disconnectOccured = true;
255             } else if (message instanceof SwitchIdleEvent) {
256                 systemListener.onSwitchIdleEvent((SwitchIdleEvent) message);
257             }
258             // OpenFlow messages
259               else if (message instanceof EchoRequestMessage) {
260                 messageListener.onEchoRequestMessage((EchoRequestMessage) message);
261             } else if (message instanceof ErrorMessage) {
262                 messageListener.onErrorMessage((ErrorMessage) message);
263             } else if (message instanceof ExperimenterMessage) {
264                 messageListener.onExperimenterMessage((ExperimenterMessage) message);
265             } else if (message instanceof FlowRemovedMessage) {
266                 messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
267             } else if (message instanceof HelloMessage) {
268                 LOG.info("Hello received / branch");
269                 messageListener.onHelloMessage((HelloMessage) message);
270             } else if (message instanceof MultipartReplyMessage) {
271                 messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
272             } else if (message instanceof PacketInMessage) {
273                 messageListener.onPacketInMessage((PacketInMessage) message);
274             } else if (message instanceof PortStatusMessage) {
275                 messageListener.onPortStatusMessage((PortStatusMessage) message);
276             } else {
277                 LOG.warn("message listening not supported for type: "+message.getClass());
278             }
279         } else {
280             if (message instanceof OfHeader) {
281                 LOG.debug("OFheader msg received");
282                 RpcResponseKey key = createRpcResponseKey((OfHeader) message);
283                 final SettableFuture<RpcResult<?>> rpcFuture = findRpcResponse(key);
284                 if (rpcFuture != null) {
285                     LOG.debug("corresponding rpcFuture found");
286                     List<RpcError> errors = Collections.emptyList();
287                     LOG.debug("before setting rpcFuture");
288                     rpcFuture.set(Rpcs.getRpcResult(true, message, errors));
289                     LOG.debug("after setting rpcFuture");
290                     responseCache.invalidate(key);
291                 } else {
292                     LOG.warn("received unexpected rpc response: "+key);
293                 }
294             } else {
295                 LOG.warn("message listening not supported for type: "+message.getClass());
296             }
297         }
298     }
299
300     /**
301      * sends given message to switch, sending result will be reported via return value
302      * @param input message to send
303      * @param failureInfo describes, what type of message caused failure by sending
304      * @return future object, <ul>
305      *  <li>if send successful, {@link RpcResult} without errors and successful
306      *  status will be returned, </li>
307      *  <li>else {@link RpcResult} will contain errors and failed status</li>
308      *  </ul>
309      */
310     private SettableFuture<RpcResult<Void>> sendToSwitchFuture(
311             DataObject input, final String failureInfo) {
312         LOG.debug("going to flush");
313         ChannelFuture resultFuture = channel.writeAndFlush(input);
314         LOG.debug("flushed");
315
316         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
317         String errorMessage = "check switch connection";
318         return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, errorMessage);
319     }
320
321     /**
322      * sends given message to switch, sending result or switch response will be reported via return value
323      * @param input message to send
324      * @param responseClazz type of response
325      * @param failureInfo describes, what type of message caused failure by sending
326      * @return future object, <ul>
327      *  <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
328      *  <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout
329      *  ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}),
330      *  <ul><li>either switch will manage to answer
331      *  and then corresponding response message will be set into returned future</li>
332      *  <li>or response in cache will expire and returned future will be cancelled</li></ul>
333      *  </li>
334      *  </ul>
335      */
336     private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
337             IN input, Class<OUT> responseClazz, final String failureInfo) {
338         LOG.debug("going to flush");
339         SettableFuture<RpcResult<OUT>> rpcResult = SettableFuture.create();
340         RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz.getName());
341         responseCache.put(key, rpcResult);
342         ChannelFuture resultFuture = channel.writeAndFlush(input);
343         LOG.debug("flushed");
344
345         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
346         String errorMessage = "check switch connection";
347
348         return handleRpcChannelFutureWithResponse(resultFuture, failureInfo, errorSeverity,
349                 errorMessage, input, responseClazz, rpcResult, key);
350     }
351
352     /**
353      * @param resultFuture
354      * @param failureInfo
355      * @return
356      */
357     private SettableFuture<RpcResult<Void>> handleRpcChannelFuture(
358             ChannelFuture resultFuture, final String failureInfo,
359             final ErrorSeverity errorSeverity, final String errorMessage) {
360
361         final SettableFuture<RpcResult<Void>> rpcResult = SettableFuture.create();
362         LOG.debug("handlerpcchannelfuture");
363         resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
364
365             @Override
366             public void operationComplete(
367                     io.netty.util.concurrent.Future<? super Void> future)
368                     throws Exception {
369                 LOG.debug("operation complete");
370                 Collection<RpcError> errors = Collections.emptyList();
371
372                 if (future.cause() != null) {
373                     LOG.debug("future.cause != null");
374                     RpcError rpcError = buildRpcError(failureInfo,
375                             errorSeverity, errorMessage, future.cause());
376                     errors = Lists.newArrayList(rpcError);
377                 }
378
379                 rpcResult.set(Rpcs.getRpcResult(
380                         future.isSuccess(),
381                         (Void) null,
382                         errors)
383                 );
384             }
385         });
386         return rpcResult;
387     }
388
389     /**
390      * @param resultFuture
391      * @param failureInfo
392      * @param errorSeverity
393      * @param errorMessage
394      * @param input
395      * @param responseClazz
396      * @param key of rpcResponse
397      * @return
398      */
399     private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> handleRpcChannelFutureWithResponse(
400             ChannelFuture resultFuture, final String failureInfo,
401             final ErrorSeverity errorSeverity, final String errorMessage,
402             final IN input, Class<OUT> responseClazz, final SettableFuture<RpcResult<OUT>> rpcResult, final RpcResponseKey key) {
403         LOG.debug("handleRpcchanfuture with response");
404
405         resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
406
407             @Override
408             public void operationComplete(
409                     io.netty.util.concurrent.Future<? super Void> future)
410                     throws Exception {
411
412                 LOG.debug("operation complete");
413                 Collection<RpcError> errors = Collections.emptyList();
414                 if (future.cause() != null) {
415                     LOG.debug("ChannelFuture.cause != null");
416                     RpcError rpcError = buildRpcError(failureInfo,
417                             errorSeverity, errorMessage, future.cause());
418                     errors = Lists.newArrayList(rpcError);
419                     rpcResult.set(Rpcs.getRpcResult(
420                             future.isSuccess(),
421                             (OUT) null,
422                             errors)
423                             );
424                     responseCache.invalidate(key);
425                 } else {
426                     LOG.debug("ChannelFuture.cause == null");
427                     if (responseCache.getIfPresent(key) == null) {
428                         LOG.debug("responcache: key wasn't present");
429                     }
430                 }
431             }
432         });
433         return rpcResult;
434     }
435
436     /**
437      * @param resultFuture
438      * @param failureInfo
439      * @param errorSeverity
440      * @param message
441      * @return
442      */
443     private static SettableFuture<Boolean> handleTransportChannelFuture(
444             ChannelFuture resultFuture, final String failureInfo,
445             final ErrorSeverity errorSeverity, final String message) {
446
447         final SettableFuture<Boolean> transportResult = SettableFuture.create();
448
449         resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
450
451             @Override
452             public void operationComplete(
453                     io.netty.util.concurrent.Future<? super Void> future)
454                     throws Exception {
455                 transportResult.set(future.isSuccess());
456                 if (!future.isSuccess()) {
457                     transportResult.setException(future.cause());
458                 }
459             }
460         });
461         return transportResult;
462     }
463
464     /**
465      * @param cause
466      * @return
467      */
468     protected RpcError buildRpcError(String info, ErrorSeverity severity, String message,
469             Throwable cause) {
470         RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
471                 ErrorType.RPC, cause);
472         return error;
473     }
474
475     /**
476      * @param cause
477      * @return
478      */
479     protected RpcError buildTransportError(String info, ErrorSeverity severity, String message,
480             Throwable cause) {
481         RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
482                 ErrorType.TRANSPORT, cause);
483         return error;
484     }
485
486     /**
487      * @param message
488      * @return
489      */
490     private static RpcResponseKey createRpcResponseKey(OfHeader message) {
491         return new RpcResponseKey(message.getXid(), message.getImplementedInterface().getName());
492     }
493
494     /**
495      * @return
496      */
497     @SuppressWarnings("unchecked")
498     private SettableFuture<RpcResult<?>> findRpcResponse(RpcResponseKey key) {
499         return (SettableFuture<RpcResult<?>>) responseCache.getIfPresent(key);
500     }
501
502     @Override
503     public void setSystemListener(SystemNotificationsListener systemListener) {
504         this.systemListener = systemListener;
505     }
506
507     @Override
508     public void checkListeners() {
509         StringBuffer buffer =  new StringBuffer();
510         if (systemListener == null) {
511             buffer.append("SystemListener ");
512         }
513         if (messageListener == null) {
514             buffer.append("MessageListener ");
515         }
516         if (connectionReadyListener == null) {
517             buffer.append("ConnectionReadyListener ");
518         }
519
520         if (buffer.length() > 0) {
521             throw new IllegalStateException("Missing listeners: " + buffer.toString());
522         }
523     }
524
525     static class ResponseRemovalListener implements RemovalListener<RpcResponseKey, SettableFuture<?>> {
526         @Override
527         public void onRemoval(
528                 RemovalNotification<RpcResponseKey, SettableFuture<?>> notification) {
529             SettableFuture<?> future = notification.getValue();
530             if (!future.isDone()) {
531                 LOG.warn("rpc response discarded: " + notification.getKey());
532                 future.cancel(true);
533             }
534         }
535     }
536
537     /**
538      * Class is used ONLY for exiting msgQueue processing thread
539      * @author michal.polkorab
540      */
541     static class ExitingDataObject implements DataObject {
542         @Override
543         public Class<? extends DataContainer> getImplementedInterface() {
544             return null;
545         }
546     }
547
548     @Override
549     public void fireConnectionReadyNotification() {
550         new Thread(new Runnable() {
551             @Override
552             public void run() {
553                 connectionReadyListener.onConnectionReady();
554             }
555         }).start();
556     }
557
558
559     @Override
560     public void setConnectionReadyListener(
561             ConnectionReadyListener connectionReadyListener) {
562         this.connectionReadyListener = connectionReadyListener;
563     }
564
565 }