Bug 5596 Created lifecycle service
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / ConnectionContextImpl.java
index bbc58a8c273b6f9aa89d855d09b2cbd21f94ddbd..aebb3fe3d3221034a1291f9f1c7688aedf0d6657 100644 (file)
@@ -1,24 +1,38 @@
 /**
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
- * <p/>
+ *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
+
 package org.opendaylight.openflowplugin.impl.connection;
 
+import com.google.common.base.Preconditions;
 import java.math.BigInteger;
 import java.net.InetSocketAddress;
+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;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
+import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -28,7 +42,7 @@ import org.slf4j.LoggerFactory;
 public class ConnectionContextImpl implements ConnectionContext {
 
     private final ConnectionAdapter connectionAdapter;
-    private CONNECTION_STATE connectionState;
+    private volatile CONNECTION_STATE connectionState;
     private FeaturesReply featuresReply;
     private NodeId nodeId;
     private DeviceDisconnectedHandler deviceDisconnectedHandler;
@@ -36,6 +50,7 @@ public class ConnectionContextImpl implements ConnectionContext {
     private OutboundQueueProvider outboundQueueProvider;
     private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
     private HandshakeContext handshakeContext;
+    private DeviceInfo deviceInfo;
 
     /**
      * @param connectionAdapter
@@ -90,18 +105,32 @@ public class ConnectionContextImpl implements ConnectionContext {
     }
 
     @Override
-    public void closeConnection(boolean propagate) {
+    public void closeConnection(final boolean propagate) {
         if (null == 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:{}.",
+        LOG.debug("Actively closing connection: {}, datapathId: {}",
                 connectionAdapter.getRemoteAddress(), datapathId);
         connectionState = ConnectionContext.CONNECTION_STATE.RIP;
 
-        unregisterOutboundQueue();
+        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);
+        }
+
         closeHandshakeContext();
 
         if (getConnectionAdapter().isAlive()) {
@@ -109,16 +138,20 @@ 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);
         if (handshakeContext != null) {
             try {
                 handshakeContext.close();
             } catch (Exception e) {
-                LOG.info("handshake context closing failed: ", e);
+                LOG.error("handshake context closing failed:{} ", e);
             } finally {
                 handshakeContext = null;
             }
@@ -167,6 +200,7 @@ public class ConnectionContextImpl implements ConnectionContext {
     }
 
     private void unregisterOutboundQueue() {
+        LOG.debug("Trying unregister outbound queue handler registration for node {}", nodeId);
         if (outboundQueueHandlerRegistration != null) {
             outboundQueueHandlerRegistration.close();
             outboundQueueHandlerRegistration = null;
@@ -174,22 +208,113 @@ public class ConnectionContextImpl implements ConnectionContext {
     }
 
     @Override
-    public void changeStateToHandshaking() {
+    public synchronized void changeStateToHandshaking() {
         connectionState = CONNECTION_STATE.HANDSHAKING;
     }
 
     @Override
-    public void changeStateToTimeouting() {
+    public synchronized void changeStateToTimeouting() {
         connectionState = CONNECTION_STATE.TIMEOUTING;
     }
 
     @Override
-    public void changeStateToWorking() {
+    public synchronized void changeStateToWorking() {
         connectionState = CONNECTION_STATE.WORKING;
     }
 
+    @Override
+    public DeviceInfo getDeviceInfo() {
+        return this.deviceInfo;
+    }
+
+    @Override
+    public void handshakeSuccessful() {
+        Preconditions.checkNotNull(nodeId, "Cannot create DeviceInfo if 'NodeId' is not set!");
+        Preconditions.checkNotNull(featuresReply, "Cannot create DeviceInfo if 'features' is not set!");
+        this.deviceInfo = new DeviceInfoImpl(
+                nodeId,
+                DeviceStateUtil.createNodeInstanceIdentifier(nodeId),
+                featuresReply.getVersion(),
+                featuresReply.getDatapathId());
+    }
+
     @Override
     public void setHandshakeContext(HandshakeContext handshakeContext) {
         this.handshakeContext = handshakeContext;
     }
+
+
+    private class DeviceInfoImpl implements DeviceInfo {
+
+        final private NodeId nodeId;
+        final private KeyedInstanceIdentifier<Node, NodeKey> nodeII;
+        final private Short version;
+        final private BigInteger datapathId;
+        final private ServiceGroupIdentifier serviceGroupIdentifier;
+
+        DeviceInfoImpl(
+                final NodeId nodeId,
+                final KeyedInstanceIdentifier<Node, NodeKey> nodeII,
+                final Short version,
+                final BigInteger datapathId) {
+            this.nodeId = nodeId;
+            this.nodeII = nodeII;
+            this.version = version;
+            this.datapathId = datapathId;
+            this.serviceGroupIdentifier = ServiceGroupIdentifier.create(this.nodeId.getValue());
+        }
+
+        @Override
+        public NodeId getNodeId() {
+            return nodeId;
+        }
+
+        @Override
+        public KeyedInstanceIdentifier<Node, NodeKey> getNodeInstanceIdentifier() {
+            return nodeII;
+        }
+
+        @Override
+        public Short getVersion() {
+            return version;
+        }
+
+        @Override
+        public BigInteger getDatapathId() {
+            return datapathId;
+        }
+
+        @Override
+        public ServiceGroupIdentifier getServiceIdentifier() {
+            return this.serviceGroupIdentifier;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+
+            DeviceInfoImpl that = (DeviceInfoImpl) o;
+
+            return  (nodeId.equals(that.nodeId) &&
+                    nodeII.equals(that.nodeII) &&
+                    version.equals(that.version) &&
+                    datapathId.equals(that.datapathId));
+
+        }
+
+        @Override
+        public int hashCode() {
+            int result = nodeId.hashCode();
+            result = 31 * result + nodeII.hashCode();
+            result = 31 * result + version.hashCode();
+            result = 31 * result + datapathId.hashCode();
+            return result;
+        }
+    }
 }