Bug:3026 - Echo response timeout needs be exported to configuration
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / SystemNotificationsListenerImpl.java
1 /**
2  * Copyright (c) 2015 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.impl.connection.listener;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import java.net.InetSocketAddress;
13 import java.util.concurrent.Future;
14 import java.util.concurrent.TimeUnit;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
24 import org.opendaylight.yangtools.yang.common.RpcError;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  *
31  */
32 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
33
34     private final ConnectionContext connectionContext;
35     private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
36     @VisibleForTesting
37     static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
38     private final long echoReplyTimeout;
39
40     public SystemNotificationsListenerImpl(@Nonnull final ConnectionContext connectionContext, long echoReplyTimeout) {
41         this.connectionContext = Preconditions.checkNotNull(connectionContext);
42         this.echoReplyTimeout = echoReplyTimeout;
43     }
44
45     @Override
46     public void onDisconnectEvent(final DisconnectEvent notification) {
47         connectionContext.onConnectionClosed();
48     }
49
50     @Override
51     public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
52         new Thread(new Runnable() {
53             @Override
54             public void run() {
55                 boolean shouldBeDisconnected = true;
56
57                 final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
58
59                 if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
60                     FeaturesReply features = connectionContext.getFeatures();
61                     LOG.debug("Switch Idle state occured, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
62                     connectionContext.changeStateToTimeouting();
63                     EchoInputBuilder builder = new EchoInputBuilder();
64                     builder.setVersion(features.getVersion());
65                     Xid xid = new Xid(0L);
66                     builder.setXid(xid.getValue());
67
68                     Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
69
70                     try {
71                         RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
72                         if (echoReplyValue.isSuccessful()) {
73                             connectionContext.changeStateToWorking();
74                             shouldBeDisconnected = false;
75                         } else {
76                             for (RpcError replyError : echoReplyValue.getErrors()) {
77                                 Throwable cause = replyError.getCause();
78                                 if (LOG.isWarnEnabled()) {
79                                     LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause.getMessage());
80                                 }
81
82                                 if (LOG.isTraceEnabled()) {
83                                     LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);
84                                 }
85
86                             }
87                         }
88                     } catch (Exception e) {
89                         if (LOG.isWarnEnabled()) {
90                             LOG.warn("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e.getMessage());
91                         }
92
93                         if (LOG.isTraceEnabled()) {
94                             LOG.trace("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e);
95                         }
96
97                     }
98                 }
99                 if (shouldBeDisconnected) {
100                     connectionContext.closeConnection(true);
101                 }
102             }
103         }).start();
104     }
105 }