Bug 5596 Created lifecycle service
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / ConnectionContextImpl.java
index a0a20a9a00d3f308310642f7542f8dee828067e3..aebb3fe3d3221034a1291f9f1c7688aedf0d6657 100644 (file)
@@ -8,6 +8,7 @@
 
 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;
@@ -16,17 +17,22 @@ 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;
 
@@ -44,6 +50,7 @@ public class ConnectionContextImpl implements ConnectionContext {
     private OutboundQueueProvider outboundQueueProvider;
     private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
     private HandshakeContext handshakeContext;
+    private DeviceInfo deviceInfo;
 
     /**
      * @param connectionAdapter
@@ -105,7 +112,7 @@ public class ConnectionContextImpl implements ConnectionContext {
             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;
 
@@ -117,15 +124,11 @@ public class ConnectionContextImpl implements ConnectionContext {
             }
         });
         try {
-            LOG.debug("Waiting 1s for unregistering outbound queue.");
             future.get(1, TimeUnit.SECONDS);
-            LOG.info("Unregistering outbound queue successful.");
-        } catch (InterruptedException e) {
-            LOG.warn("Unregistering outbound queue was interrupted for node {}", nodeId);
-        } catch (ExecutionException e) {
-            LOG.warn("Unregistering outbound queue throws exception for node {}", nodeId, e);
-        } catch (TimeoutException e) {
-            LOG.warn("Unregistering outbound queue took longer than 1 seconds for node {}", nodeId);
+            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();
@@ -148,7 +151,7 @@ public class ConnectionContextImpl implements ConnectionContext {
             try {
                 handshakeContext.close();
             } catch (Exception e) {
-                LOG.info("handshake context closing failed: ", e);
+                LOG.error("handshake context closing failed:{} ", e);
             } finally {
                 handshakeContext = null;
             }
@@ -219,8 +222,99 @@ public class ConnectionContextImpl implements ConnectionContext {
         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;
+        }
+    }
 }