Fix SwitchIdleEvent echo request-reply xid
[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.Date;
14 import java.util.Objects;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.ThreadPoolExecutor;
17 import java.util.concurrent.TimeUnit;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
27 import org.opendaylight.yangtools.yang.common.RpcError;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  *
34  */
35 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
36
37     private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
38     private static final Xid ECHO_XID = new Xid(0L);
39
40     private final ConnectionContext connectionContext;
41     @VisibleForTesting
42     static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
43     private final long echoReplyTimeout;
44     private final ThreadPoolExecutor threadPool;
45
46     public SystemNotificationsListenerImpl(@Nonnull final ConnectionContext connectionContext,
47                                            long echoReplyTimeout,
48                                            @Nonnull final ThreadPoolExecutor threadPool) {
49         this.threadPool = threadPool;
50         this.connectionContext = Preconditions.checkNotNull(connectionContext);
51         this.echoReplyTimeout = echoReplyTimeout;
52     }
53
54     @Override
55     public void onDisconnectEvent(final DisconnectEvent notification) {
56         LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}",
57                 connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getSafeNodeIdForLOG());
58         connectionContext.onConnectionClosed();
59     }
60
61     @Override
62     public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
63         threadPool.execute(this::executeOnSwitchIdleEvent);
64     }
65
66     private void executeOnSwitchIdleEvent() {
67         boolean shouldBeDisconnected = true;
68
69         final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
70
71         if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
72             FeaturesReply features = connectionContext.getFeatures();
73             LOG.info("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
74             connectionContext.changeStateToTimeouting();
75             EchoInputBuilder builder = new EchoInputBuilder();
76             builder.setVersion(features.getVersion());
77             builder.setXid(ECHO_XID.getValue());
78
79             Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
80
81             try {
82                 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
83                 if (echoReplyValue.isSuccessful() &&
84                         Objects.equals(echoReplyValue.getResult().getXid(), ECHO_XID.getValue())) {
85                     connectionContext.changeStateToWorking();
86                     shouldBeDisconnected = false;
87                 } else {
88                     logErrors(remoteAddress, echoReplyValue);
89                 }
90             } catch (Exception e) {
91                 if (LOG.isWarnEnabled()) {
92                     LOG.warn("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e.getMessage());
93                 }
94
95                 if (LOG.isTraceEnabled()) {
96                     LOG.trace("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e);
97                 }
98
99             }
100         }
101         if (shouldBeDisconnected) {
102             if (LOG.isInfoEnabled()) {
103                 LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
104                         new Date(System.currentTimeMillis() - echoReplyTimeout), remoteAddress, connectionContext.getSafeNodeIdForLOG());
105             }
106
107             connectionContext.closeConnection(true);
108         }
109     }
110
111     private void logErrors(InetSocketAddress remoteAddress, RpcResult<EchoOutput> echoReplyValue) {
112         for (RpcError replyError : echoReplyValue.getErrors()) {
113             Throwable cause = replyError.getCause();
114             if (LOG.isWarnEnabled()) {
115                 LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause.getMessage());
116             }
117
118             if (LOG.isTraceEnabled()) {
119                 LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);
120             }
121
122         }
123     }
124 }