Optimize port number lookups
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / ConnectionContextImpl.java
index dea0a5e6818b424021bc293bdb6635e1c706301b..f68ec73839122308202f38fe7268b3b1834dea15 100644 (file)
@@ -12,12 +12,6 @@ import com.google.common.base.Preconditions;
 import java.math.BigInteger;
 import java.net.InetSocketAddress;
 import java.util.Objects;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
@@ -108,31 +102,19 @@ public class ConnectionContextImpl implements ConnectionContext {
 
     @Override
     public void closeConnection(final boolean propagate) {
-        if (null == nodeId){
+        if (Objects.isNull(nodeId)){
             SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
         } else {
             SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
         }
-        final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
-        LOG.debug("Actively closing connection: {}, datapathId: {}",
-                connectionAdapter.getRemoteAddress(), datapathId);
-        connectionState = ConnectionContext.CONNECTION_STATE.RIP;
-
-        Future<Void> future = Executors.newSingleThreadExecutor().submit(new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                unregisterOutboundQueue();
-                return null;
-            }
-        });
-        try {
-            future.get(1, TimeUnit.SECONDS);
-            LOG.info("Unregister outbound queue successful.");
-        } catch (InterruptedException | TimeoutException | ExecutionException e) {
-            LOG.warn("Unregister outbound queue throws exception for node {} ", nodeId);
-            LOG.trace("Unregister outbound queue throws exception for node {} ", nodeId, e);
+        final BigInteger datapathId = Objects.nonNull(featuresReply) ? featuresReply.getDatapathId() : BigInteger.ZERO;
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Actively closing connection: {}, datapathId: {}",
+                    connectionAdapter.getRemoteAddress(), datapathId);
         }
+        connectionState = ConnectionContext.CONNECTION_STATE.RIP;
 
+        unregisterOutboundQueue();
         closeHandshakeContext();
 
         if (getConnectionAdapter().isAlive()) {
@@ -140,15 +122,12 @@ public class ConnectionContextImpl implements ConnectionContext {
         }
 
         if (propagate) {
-            LOG.debug("Propagating device disconnect for node {}", nodeId);
             propagateDeviceDisconnectedEvent();
-        } else {
-            LOG.debug("Close connection without propagating for node {}", nodeId);
         }
     }
 
     private void closeHandshakeContext() {
-        LOG.debug("Trying closing handshake context for node {}", nodeId);
+        LOG.debug("Trying closing handshake context for node {}", getSafeNodeIdForLOG());
         if (handshakeContext != null) {
             try {
                 handshakeContext.close();
@@ -162,12 +141,14 @@ public class ConnectionContextImpl implements ConnectionContext {
 
     @Override
     public void onConnectionClosed() {
+
+        connectionState = ConnectionContext.CONNECTION_STATE.RIP;
+
         if (null == nodeId){
             SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
         } else {
             SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
         }
-        connectionState = ConnectionContext.CONNECTION_STATE.RIP;
 
         final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
         final Short auxiliaryId;
@@ -188,21 +169,34 @@ public class ConnectionContextImpl implements ConnectionContext {
     }
 
     private void propagateDeviceDisconnectedEvent() {
-        if (null != deviceDisconnectedHandler) {
+        if (Objects.nonNull(deviceDisconnectedHandler)) {
             final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
-            LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
-                    connectionAdapter.getRemoteAddress(), datapathId);
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
+                        connectionAdapter.getRemoteAddress(), datapathId);
+            }
             deviceDisconnectedHandler.onDeviceDisconnected(this);
         }
     }
 
+    /**
+     * This method returns safe nodeId for logging
+     * @return string value od nodeId or string "null"
+     */
+    @Override
+    public String getSafeNodeIdForLOG() {
+        return Objects.nonNull(nodeId) ? nodeId.getValue() : "null";
+    }
+
     @Override
     public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
         this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
     }
 
     private void unregisterOutboundQueue() {
-        LOG.debug("Trying unregister outbound queue handler registration for node {}", nodeId);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Trying unregister outbound queue handler registration for node {}", nodeId);
+        }
         if (outboundQueueHandlerRegistration != null) {
             outboundQueueHandlerRegistration.close();
             outboundQueueHandlerRegistration = null;
@@ -312,7 +306,7 @@ public class ConnectionContextImpl implements ConnectionContext {
         }
 
         @Override
-        public Short getVersion() {
+        public short getVersion() {
             return version;
         }