Cleanup FlowMessageSerializer
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / ConnectionContextImpl.java
index 8b58e5b475adebc5373c1dcaa6addee43e476d7c..95f20df881e3f329d12a4d95163347354332af42 100644 (file)
@@ -1,26 +1,23 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * 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.MoreObjects;
 import com.google.common.base.Preconditions;
-import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.Objects;
-import java.util.Optional;
 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.DeviceConnectionStatusProvider;
 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
@@ -33,6 +30,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint64;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -49,14 +48,17 @@ public class ConnectionContextImpl implements ConnectionContext {
     private HandshakeContext handshakeContext;
     private DeviceInfo deviceInfo;
     private final List<PortStatusMessage> portStatusMessages = new ArrayList<>();
+    private final DeviceConnectionStatusProvider deviceConnectionStatusProvider;
 
     /**
      * Constructor.
      *
      * @param connectionAdapter - connection adapter
      */
-    public ConnectionContextImpl(final ConnectionAdapter connectionAdapter) {
+    public ConnectionContextImpl(final ConnectionAdapter connectionAdapter,
+                                 final DeviceConnectionStatusProvider deviceConnectionStatusProvider) {
         this.connectionAdapter = connectionAdapter;
+        this.deviceConnectionStatusProvider = deviceConnectionStatusProvider;
     }
 
     @Override
@@ -125,13 +127,15 @@ public class ConnectionContextImpl implements ConnectionContext {
 
     private void disconnectDevice(final boolean propagate,
                                   final boolean forced) {
-        final String device =
-                Objects.nonNull(nodeId) ? nodeId.getValue() : getConnectionAdapter().getRemoteAddress().toString();
-        final short auxiliaryId = Optional
-                .ofNullable(getFeatures())
-                .flatMap(features -> Optional
-                        .ofNullable(features.getAuxiliaryId()))
-                .orElse((short) 0);
+        final String device = nodeId != null ? nodeId.getValue() : getConnectionAdapter().getRemoteAddress().toString();
+
+        final Uint8 auxiliaryId;
+        if (featuresReply != null) {
+            final Uint8 id = featuresReply.getAuxiliaryId();
+            auxiliaryId = id == null ? Uint8.ZERO : id;
+        } else {
+            auxiliaryId = Uint8.ZERO;
+        }
 
         if (connectionState == CONNECTION_STATE.RIP) {
             LOG.debug("Connection for device {} with auxiliary ID {} is already {}, so skipping closing.",
@@ -165,8 +169,8 @@ public class ConnectionContextImpl implements ConnectionContext {
     }
 
     private void propagateDeviceDisconnectedEvent() {
-        if (Objects.nonNull(deviceDisconnectedHandler)) {
-            final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
+        if (deviceDisconnectedHandler != null) {
+            final Uint64 datapathId = featuresReply != null ? featuresReply.getDatapathId() : Uint64.ZERO;
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
                         connectionAdapter.getRemoteAddress(), datapathId);
@@ -182,12 +186,12 @@ public class ConnectionContextImpl implements ConnectionContext {
      */
     @Override
     public String getSafeNodeIdForLOG() {
-        return Objects.nonNull(nodeId) ? nodeId.getValue() : "null";
+        return nodeId == null ? "null" : nodeId.getValue();
     }
 
     @Override
     public void setOutboundQueueHandleRegistration(
-            OutboundQueueHandlerRegistration<OutboundQueueProvider> newRegistration) {
+            final OutboundQueueHandlerRegistration<OutboundQueueProvider> newRegistration) {
         this.outboundQueueHandlerRegistration = newRegistration;
     }
 
@@ -252,12 +256,12 @@ public class ConnectionContextImpl implements ConnectionContext {
     }
 
     @Override
-    public void setHandshakeContext(HandshakeContext handshakeContext) {
+    public void setHandshakeContext(final HandshakeContext handshakeContext) {
         this.handshakeContext = handshakeContext;
     }
 
     @Override
-    public boolean equals(Object object) {
+    public boolean equals(final Object object) {
         if (this == object) {
             return true;
         }
@@ -292,16 +296,16 @@ public class ConnectionContextImpl implements ConnectionContext {
 
         private final NodeId nodeId;
         private final KeyedInstanceIdentifier<Node, NodeKey> nodeII;
-        private final Short version;
-        private final BigInteger datapathId;
+        private final Uint8 version;
+        private final Uint64 datapathId;
         private final ServiceGroupIdentifier serviceGroupIdentifier;
         private OutboundQueue outboundQueueProvider;
 
         DeviceInfoImpl(
                 final NodeId nodeId,
                 final KeyedInstanceIdentifier<Node, NodeKey> nodeII,
-                final Short version,
-                final BigInteger datapathId,
+                final Uint8 version,
+                final Uint64 datapathId,
                 final OutboundQueue outboundQueueProvider) {
             this.nodeId = nodeId;
             this.nodeII = nodeII;
@@ -323,11 +327,11 @@ public class ConnectionContextImpl implements ConnectionContext {
 
         @Override
         public short getVersion() {
-            return version;
+            return version.toJava();
         }
 
         @Override
-        public BigInteger getDatapathId() {
+        public Uint64 getDatapathId() {
             return datapathId;
         }
 
@@ -337,7 +341,7 @@ public class ConnectionContextImpl implements ConnectionContext {
         }
 
         @Override
-        public boolean equals(Object object) {
+        public boolean equals(final Object object) {
             if (this == object) {
                 return true;
             }
@@ -366,7 +370,7 @@ public class ConnectionContextImpl implements ConnectionContext {
 
         @Override
         public String toString() {
-            return Objects.isNull(nodeId) ? "null" : getNodeId().getValue();
+            return nodeId == null ? "null" : getNodeId().getValue();
         }
 
         public void setOutboundQueueProvider(final OutboundQueue outboundQueueProvider) {