Bug 5924 - Reuse Threads using ThreadPool in SystemNotificationListenerImpl
[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.getNodeId());
55         connectionContext.onConnectionClosed();
56     }
57
58     @Override
59     public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
60         threadPool.execute(() -> {
61             boolean shouldBeDisconnected = true;
62
63             final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
64
65             if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
66                 FeaturesReply features = connectionContext.getFeatures();
67                 LOG.info("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
68                 connectionContext.changeStateToTimeouting();
69                 EchoInputBuilder builder = new EchoInputBuilder();
70                 builder.setVersion(features.getVersion());
71                 Xid xid = new Xid(0L);
72                 builder.setXid(xid.getValue());
73
74                 Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
75
76                 try {
77                     RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
78                     if (echoReplyValue.isSuccessful()) {
79                         connectionContext.changeStateToWorking();
80                         shouldBeDisconnected = false;
81                     } else {
82                         for (RpcError replyError : echoReplyValue.getErrors()) {
83                             Throwable cause = replyError.getCause();
84                             if (LOG.isWarnEnabled()) {
85                                 LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause.getMessage());
86                             }
87
88                             if (LOG.isTraceEnabled()) {
89                                 LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);
90                             }
91
92                         }
93                     }
94                 } catch (Exception e) {
95                     if (LOG.isWarnEnabled()) {
96                         LOG.warn("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e.getMessage());
97                     }
98
99                     if (LOG.isTraceEnabled()) {
100                         LOG.trace("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e);
101                     }
102
103                 }
104             }
105             if (shouldBeDisconnected) {
106                 if (LOG.isInfoEnabled()) {
107                     LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
108                             new Date(System.currentTimeMillis() - echoReplyTimeout), remoteAddress, connectionContext.getNodeId());
109                 }
110
111                 connectionContext.closeConnection(true);
112             }
113         });
114     }
115 }