X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Fconnection%2FConnectionContextImpl.java;h=e0ff00efe0424d2feb40d49fb8a5de559688e098;hb=5eb3934ab14beb9646f039e09f698dd1b118fb9b;hp=191ea623ff3315e0dfc397a9cb1f1c03008bcbe1;hpb=1f2f4d39bcd7b0fc141a593d0e03cc62cc0abfc8;p=openflowplugin.git diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/ConnectionContextImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/ConnectionContextImpl.java index 191ea623ff..e0ff00efe0 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/ConnectionContextImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/ConnectionContextImpl.java @@ -8,25 +8,32 @@ package org.opendaylight.openflowplugin.impl.connection; +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; 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 +51,7 @@ public class ConnectionContextImpl implements ConnectionContext { private OutboundQueueProvider outboundQueueProvider; private OutboundQueueHandlerRegistration outboundQueueHandlerRegistration; private HandshakeContext handshakeContext; + private DeviceInfo deviceInfo; /** * @param connectionAdapter @@ -65,6 +73,7 @@ public class ConnectionContextImpl implements ConnectionContext { @Override public void setOutboundQueueProvider(final OutboundQueueProvider outboundQueueProvider) { this.outboundQueueProvider = outboundQueueProvider; + ((DeviceInfoImpl)this.deviceInfo).setOutboundQueueProvider(this.outboundQueueProvider); } @Override @@ -99,35 +108,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 future = Executors.newSingleThreadExecutor().submit(new Callable() { - @Override - public Void call() throws Exception { - unregisterOutboundQueue(); - return null; - } - }); - 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); + 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()) { @@ -135,15 +128,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(); @@ -157,12 +147,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; @@ -183,21 +175,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 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; @@ -219,8 +224,143 @@ 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(), + outboundQueueProvider); + } + @Override public void setHandshakeContext(HandshakeContext handshakeContext) { this.handshakeContext = handshakeContext; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + ConnectionContextImpl that = (ConnectionContextImpl) o; + + if (!connectionAdapter.equals(that.connectionAdapter)) { + return false; + } + + if (featuresReply != null ? !featuresReply.equals(that.featuresReply) : that.featuresReply != null) { + return false; + } + + return nodeId != null ? nodeId.equals(that.nodeId) : that.nodeId == null; + + } + + @Override + public int hashCode() { + int result = connectionAdapter.hashCode(); + result = 31 * result + (featuresReply != null ? featuresReply.hashCode() : 0); + result = 31 * result + (nodeId != null ? nodeId.hashCode() : 0); + return result; + } + + private class DeviceInfoImpl implements DeviceInfo { + + private final NodeId nodeId; + private final KeyedInstanceIdentifier nodeII; + private final Short version; + private final BigInteger datapathId; + private final ServiceGroupIdentifier serviceGroupIdentifier; + private OutboundQueue outboundQueueProvider; + + DeviceInfoImpl( + final NodeId nodeId, + final KeyedInstanceIdentifier nodeII, + final Short version, + final BigInteger datapathId, + final OutboundQueue outboundQueueProvider) { + this.nodeId = nodeId; + this.nodeII = nodeII; + this.version = version; + this.datapathId = datapathId; + this.outboundQueueProvider = outboundQueueProvider; + this.serviceGroupIdentifier = ServiceGroupIdentifier.create(this.nodeId.getValue()); + } + + @Override + public NodeId getNodeId() { + return nodeId; + } + + @Override + public KeyedInstanceIdentifier 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; + } + + public void setOutboundQueueProvider(final OutboundQueue outboundQueueProvider) { + this.outboundQueueProvider = outboundQueueProvider; + } + + @Override + public Long reserveXidForDeviceMessage() { + return outboundQueueProvider.reserveEntry(); + } + } }