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