Fix checkstyle
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / SystemNotificationsListenerImpl.java
index 1bf37f17825be7b3456f2030a921f197ab4f1513..5084109fb1129dadba6ba8ac510ccdeb790f7827 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -11,8 +11,9 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import java.net.InetSocketAddress;
 import java.util.Date;
+import java.util.Objects;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
-import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import javax.annotation.Nonnull;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
@@ -28,38 +29,40 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/**
- *
- */
 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
 
-    private final ConnectionContext connectionContext;
     private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
+    private static final Logger OF_EVENT_LOG = LoggerFactory.getLogger("OfEventLog");
+    private static final Xid ECHO_XID = new Xid(0L);
+
+    private final ConnectionContext connectionContext;
     @VisibleForTesting
     static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
     private final long echoReplyTimeout;
-    private final ThreadPoolExecutor threadPool;
+    private final ExecutorService executorService;
 
     public SystemNotificationsListenerImpl(@Nonnull final ConnectionContext connectionContext,
                                            long echoReplyTimeout,
-                                           @Nonnull final ThreadPoolExecutor threadPool) {
-        this.threadPool = threadPool;
+                                           @Nonnull final ExecutorService executorService) {
+        this.executorService = executorService;
         this.connectionContext = Preconditions.checkNotNull(connectionContext);
         this.echoReplyTimeout = echoReplyTimeout;
     }
 
     @Override
     public void onDisconnectEvent(final DisconnectEvent notification) {
+        OF_EVENT_LOG.debug("Disconnect, Node: {}", connectionContext.getSafeNodeIdForLOG());
         LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}",
-                connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getNodeId());
+                connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getSafeNodeIdForLOG());
         connectionContext.onConnectionClosed();
     }
 
     @Override
     public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
-        threadPool.execute(this::executeOnSwitchIdleEvent);
+        executorService.execute(this::executeOnSwitchIdleEvent);
     }
 
+    @SuppressWarnings("checkstyle:IllegalCatch")
     private void executeOnSwitchIdleEvent() {
         boolean shouldBeDisconnected = true;
 
@@ -67,18 +70,20 @@ public class SystemNotificationsListenerImpl implements SystemNotificationsListe
 
         if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
             FeaturesReply features = connectionContext.getFeatures();
-            LOG.info("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
+            LOG.debug("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
+            OF_EVENT_LOG.debug("Switch idle state, Node: {}", features.getDatapathId());
             connectionContext.changeStateToTimeouting();
             EchoInputBuilder builder = new EchoInputBuilder();
             builder.setVersion(features.getVersion());
-            Xid xid = new Xid(0L);
-            builder.setXid(xid.getValue());
+            builder.setXid(ECHO_XID.getValue());
 
-            Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
+            Future<RpcResult<EchoOutput>> echoReplyFuture =
+                    connectionContext.getConnectionAdapter().echo(builder.build());
 
             try {
                 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
-                if (echoReplyValue.isSuccessful()) {
+                if (echoReplyValue.isSuccessful()
+                        && Objects.equals(echoReplyValue.getResult().getXid(), ECHO_XID.getValue())) {
                     connectionContext.changeStateToWorking();
                     shouldBeDisconnected = false;
                 } else {
@@ -86,11 +91,13 @@ public class SystemNotificationsListenerImpl implements SystemNotificationsListe
                 }
             } catch (Exception e) {
                 if (LOG.isWarnEnabled()) {
-                    LOG.warn("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e.getMessage());
+                    LOG.warn("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}",
+                            remoteAddress, e.getMessage());
                 }
 
                 if (LOG.isTraceEnabled()) {
-                    LOG.trace("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e);
+                    LOG.trace("Exception while  waiting for echoReply from [{}] in TIMEOUTING state",
+                            remoteAddress, e);
                 }
 
             }
@@ -98,7 +105,8 @@ public class SystemNotificationsListenerImpl implements SystemNotificationsListe
         if (shouldBeDisconnected) {
             if (LOG.isInfoEnabled()) {
                 LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
-                        new Date(System.currentTimeMillis() - echoReplyTimeout), remoteAddress, connectionContext.getNodeId());
+                        new Date(System.currentTimeMillis() - echoReplyTimeout),
+                        remoteAddress, connectionContext.getSafeNodeIdForLOG());
             }
 
             connectionContext.closeConnection(true);
@@ -109,13 +117,13 @@ public class SystemNotificationsListenerImpl implements SystemNotificationsListe
         for (RpcError replyError : echoReplyValue.getErrors()) {
             Throwable cause = replyError.getCause();
             if (LOG.isWarnEnabled()) {
-                LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause.getMessage());
+                LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}",
+                        remoteAddress, cause.getMessage());
             }
 
             if (LOG.isTraceEnabled()) {
-                LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);
+                LOG.trace("Received EchoReply from [{}] in TIMEOUTING state", remoteAddress, cause);
             }
-
         }
     }
 }