-/**
+/*
* 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.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 java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+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;
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.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;
-/**
- *
- */
public class ConnectionContextImpl implements ConnectionContext {
private final ConnectionAdapter connectionAdapter;
private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
private HandshakeContext handshakeContext;
private DeviceInfo deviceInfo;
+ private final List<PortStatusMessage> portStatusMessages = new ArrayList<>();
+ private final DeviceConnectionStatusProvider deviceConnectionStatusProvider;
/**
- * @param connectionAdapter
+ * 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
@Override
public void setOutboundQueueProvider(final OutboundQueueProvider outboundQueueProvider) {
this.outboundQueueProvider = outboundQueueProvider;
+ ((DeviceInfoImpl)this.deviceInfo).setOutboundQueueProvider(this.outboundQueueProvider);
}
@Override
}
@Override
- public void setFeatures(final FeaturesReply featuresReply) {
- this.featuresReply = featuresReply;
+ public void setFeatures(final FeaturesReply newFeaturesReply) {
+ this.featuresReply = newFeaturesReply;
}
@Override
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:{}.",
- 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 {
- 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);
- }
-
- closeHandshakeContext();
-
- if (getConnectionAdapter().isAlive()) {
- getConnectionAdapter().disconnect();
- }
-
- if (propagate) {
- LOG.debug("Propagating device disconnect for node {}", nodeId);
- propagateDeviceDisconnectedEvent();
- } else {
- LOG.debug("Close connection without propagating for node {}", nodeId);
- }
+ disconnectDevice(propagate, true);
}
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();
- } catch (Exception e) {
- LOG.error("handshake context closing failed:{} ", e);
- } finally {
- handshakeContext = null;
- }
+ handshakeContext.close();
+ handshakeContext = null;
}
}
@Override
public void onConnectionClosed() {
- if (null == nodeId){
- SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
+ disconnectDevice(true, false);
+ }
+
+ private void disconnectDevice(final boolean propagate,
+ final boolean forced) {
+ 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 {
- SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
+ auxiliaryId = Uint8.ZERO;
}
- connectionState = ConnectionContext.CONNECTION_STATE.RIP;
- final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
- final Short auxiliaryId;
- if (null != getFeatures() && null != getFeatures().getAuxiliaryId()) {
- auxiliaryId = getFeatures().getAuxiliaryId();
- } else {
- auxiliaryId = 0;
+ if (connectionState == CONNECTION_STATE.RIP) {
+ LOG.debug("Connection for device {} with auxiliary ID {} is already {}, so skipping closing.",
+ device, auxiliaryId, getConnectionState());
+ return;
}
- LOG.debug("disconnecting: node={}|auxId={}|connection state = {}",
- remoteAddress,
+ connectionState = ConnectionContext.CONNECTION_STATE.RIP;
+
+ SessionStatistics.countEvent(device, forced
+ ? SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP
+ : SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
+
+ LOG.debug("{}: device={} | auxiliaryId={} | connectionState={}",
+ forced ? "Actively closing connection" : "Disconnecting",
+ device,
auxiliaryId,
getConnectionState());
+ portStatusMessages.clear();
unregisterOutboundQueue();
closeHandshakeContext();
- propagateDeviceDisconnectedEvent();
+
+ if (forced && getConnectionAdapter().isAlive()) {
+ getConnectionAdapter().disconnect();
+ }
+
+ if (propagate) {
+ propagateDeviceDisconnectedEvent();
+ }
}
private void propagateDeviceDisconnectedEvent() {
- if (null != deviceDisconnectedHandler) {
- final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
- LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
- connectionAdapter.getRemoteAddress(), datapathId);
+ 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);
+ }
deviceDisconnectedHandler.onDeviceDisconnected(this);
}
}
+ /**
+ * Get safe nodeId for logging.
+ *
+ * @return string value od nodeId or string "null"
+ */
@Override
- public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
- this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
+ public String getSafeNodeIdForLOG() {
+ return nodeId == null ? "null" : nodeId.getValue();
+ }
+
+ @Override
+ public void setOutboundQueueHandleRegistration(
+ final OutboundQueueHandlerRegistration<OutboundQueueProvider> newRegistration) {
+ this.outboundQueueHandlerRegistration = newRegistration;
}
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 {}", getSafeNodeIdForLOG());
+ }
if (outboundQueueHandlerRegistration != null) {
outboundQueueHandlerRegistration.close();
outboundQueueHandlerRegistration = null;
connectionState = CONNECTION_STATE.WORKING;
}
+ @Override
+ public void handlePortStatusMessage(final PortStatusMessage portStatusMessage) {
+ LOG.info("Received early port status message for node {} with reason {} and state {}",
+ getSafeNodeIdForLOG(),
+ portStatusMessage.getReason(),
+ MoreObjects.firstNonNull(portStatusMessage.getState(), portStatusMessage.getStateV10()));
+
+ LOG.debug("Early port status message body is {}", portStatusMessage);
+ portStatusMessages.add(portStatusMessage);
+ }
+
+ @Override
+ public List<PortStatusMessage> retrieveAndClearPortStatusMessages() {
+ final List<PortStatusMessage> immutablePortStatusMessages = Collections.unmodifiableList(portStatusMessages);
+ portStatusMessages.clear();
+ return immutablePortStatusMessages;
+ }
+
@Override
public DeviceInfo getDeviceInfo() {
return this.deviceInfo;
nodeId,
DeviceStateUtil.createNodeInstanceIdentifier(nodeId),
featuresReply.getVersion(),
- featuresReply.getDatapathId());
+ featuresReply.getDatapathId(),
+ outboundQueueProvider);
}
@Override
- public void setHandshakeContext(HandshakeContext handshakeContext) {
+ public void setHandshakeContext(final HandshakeContext handshakeContext) {
this.handshakeContext = handshakeContext;
}
+ @Override
+ public boolean equals(final Object object) {
+ if (this == object) {
+ return true;
+ }
+
+ if (object == null || getClass() != object.getClass()) {
+ return false;
+ }
+
+ ConnectionContextImpl that = (ConnectionContextImpl) object;
+
+ 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 static class DeviceInfoImpl implements DeviceInfo {
- final private NodeId nodeId;
- final private KeyedInstanceIdentifier<Node, NodeKey> nodeII;
- final private Short version;
- final private BigInteger datapathId;
+ private final NodeId nodeId;
+ private final KeyedInstanceIdentifier<Node, NodeKey> nodeII;
+ 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;
this.version = version;
this.datapathId = datapathId;
+ this.outboundQueueProvider = outboundQueueProvider;
+ this.serviceGroupIdentifier = ServiceGroupIdentifier.create(this.nodeId.getValue());
}
@Override
}
@Override
- public Short getVersion() {
- return version;
+ public short getVersion() {
+ return version.toJava();
}
@Override
- public BigInteger getDatapathId() {
+ public Uint64 getDatapathId() {
return datapathId;
}
@Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
+ public ServiceGroupIdentifier getServiceIdentifier() {
+ return this.serviceGroupIdentifier;
+ }
- DeviceInfoImpl that = (DeviceInfoImpl) o;
+ @Override
+ public boolean equals(final Object object) {
+ if (this == object) {
+ return true;
+ }
+
+ if (object == null || getClass() != object.getClass()) {
+ return false;
+ }
- if (!nodeId.equals(that.nodeId)) return false;
- if (!nodeII.equals(that.nodeII)) return false;
- if (!version.equals(that.version)) return false;
- return datapathId.equals(that.datapathId);
+ DeviceInfoImpl that = (DeviceInfoImpl) object;
+
+ return nodeId.equals(that.nodeId)
+ && nodeII.equals(that.nodeII)
+ && version.equals(that.version)
+ && datapathId.equals(that.datapathId);
}
result = 31 * result + datapathId.hashCode();
return result;
}
+
+ @Override
+ public String toString() {
+ return nodeId == null ? "null" : getNodeId().getValue();
+ }
+
+ public void setOutboundQueueProvider(final OutboundQueue outboundQueueProvider) {
+ this.outboundQueueProvider = outboundQueueProvider;
+ }
+
+ @Override
+ public Long reserveXidForDeviceMessage() {
+ return outboundQueueProvider.reserveEntry();
+ }
}
}