package org.opendaylight.openflowplugin.openflow.md.core;
-import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.concurrent.Future;
-import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
+import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
+import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeper;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
+import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.common.RpcError;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Futures;
/**
* @author mirehak
*/
public class ConnectionConductorImpl implements OpenflowProtocolListener,
- SystemNotificationsListener, ConnectionConductor {
+ SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener {
- private static final Logger LOG = LoggerFactory
+ protected static final Logger LOG = LoggerFactory
.getLogger(ConnectionConductorImpl.class);
- private LinkedBlockingQueue<Exception> errorQueue = new LinkedBlockingQueue<>();
+ /* variable to make BitMap-based negotiation enabled / disabled.
+ * it will help while testing and isolating issues related to processing of
+ * BitMaps from switches.
+ */
+ protected boolean isBitmapNegotiationEnable = true;
+ protected ErrorHandler errorHandler;
private final ConnectionAdapter connectionAdapter;
- private final List<Short> versionOrder;
private ConnectionConductor.CONDUCTOR_STATE conductorState;
private Short version;
private SessionContext sessionContext;
+ protected boolean isFirstHelloNegotiation = true;
+ protected Short lastProposedVersion = null;
+
+ private QueueKeeper<OfHeader, DataObject> queueKeeper;
+ private ExecutorService hsPool;
+ private HandshakeManager handshakeManager;
+
/**
* @param connectionAdapter
*/
public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
this.connectionAdapter = connectionAdapter;
conductorState = CONDUCTOR_STATE.HANDSHAKING;
- versionOrder = Lists.newArrayList((short) 0x04, (short) 0x01);
- new Thread(new ErrorQueueHandler(errorQueue)).start();
+ hsPool = Executors.newFixedThreadPool(1);
+ handshakeManager = new HandshakeManagerImpl(connectionAdapter,
+ ConnectionConductor.versionOrder.get(0), ConnectionConductor.versionOrder);
+ handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnable);
+ handshakeManager.setHandshakeListener(this);
}
@Override
public void init() {
connectionAdapter.setMessageListener(this);
connectionAdapter.setSystemListener(this);
+ connectionAdapter.setConnectionReadyListener(this);
+ }
+
+ @Override
+ public void setQueueKeeper(QueueKeeper<OfHeader, DataObject> queueKeeper) {
+ this.queueKeeper = queueKeeper;
}
+ /**
+ * @param errorHandler the errorHandler to set
+ */
@Override
- public void onEchoRequestMessage(EchoRequestMessage echoRequestMessage) {
- LOG.debug("echo request received: " + echoRequestMessage.getXid());
- EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
- builder.setVersion(echoRequestMessage.getVersion());
- builder.setXid(echoRequestMessage.getXid());
- builder.setData(echoRequestMessage.getData());
-
- connectionAdapter.echoReply(builder.build());
+ public void setErrorHandler(ErrorHandler errorHandler) {
+ this.errorHandler = errorHandler;
+ handshakeManager.setErrorHandler(errorHandler);
+ }
+
+ @Override
+ public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ LOG.debug("echo request received: " + echoRequestMessage.getXid());
+ EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
+ builder.setVersion(echoRequestMessage.getVersion());
+ builder.setXid(echoRequestMessage.getXid());
+ builder.setData(echoRequestMessage.getData());
+
+ getConnectionAdapter().echoReply(builder.build());
+ }
+ }).start();
}
@Override
public void onErrorMessage(ErrorMessage errorMessage) {
- // TODO Auto-generated method stub
- LOG.debug("error received, type: " + errorMessage.getType()
- + "; code: " + errorMessage.getCode());
+ queueKeeper.push(errorMessage, this);
}
@Override
public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
- // TODO Auto-generated method stub
- LOG.debug("experimenter received, type: "
- + experimenterMessage.getExpType());
+ queueKeeper.push(experimenterMessage, this);
}
@Override
- public void onFlowRemovedMessage(FlowRemovedMessage arg0) {
- // TODO Auto-generated method stub
+ public void onFlowRemovedMessage(FlowRemovedMessage message) {
+ queueKeeper.push(message, this);
}
+
+ /**
+ * version negotiation happened as per following steps:
+ * 1. If HelloMessage version field has same version, continue connection processing.
+ * If HelloMessage version is lower than supported versions, just disconnect.
+ * 2. If HelloMessage contains bitmap and common version found in bitmap
+ * then continue connection processing. if no common version found, just disconnect.
+ * 3. If HelloMessage version is not supported, send HelloMessage with lower supported version.
+ * 4. If Hello message received again with not supported version, just disconnect.
+ *
+ * TODO: Better to handle handshake into a maintainable innerclass which uses State-Pattern.
+ */
@Override
- public void onHelloMessage(HelloMessage hello) {
- // do handshake
- LOG.info("handshake STARTED");
+ public synchronized void onHelloMessage(final HelloMessage hello) {
+ LOG.debug("processing HELLO.xid{}", hello.getXid());
checkState(CONDUCTOR_STATE.HANDSHAKING);
-
- Short remoteVersion = hello.getVersion();
- short proposedVersion;
- try {
- proposedVersion = proposeVersion(remoteVersion);
- } catch (Exception e) {
- handleException(e);
- throw e;
- }
- HelloInputBuilder helloBuilder = new HelloInputBuilder();
- helloBuilder.setVersion(proposedVersion).setXid(hello.getXid());
- LOG.debug("sending helloReply");
- connectionAdapter.hello(helloBuilder.build());
-
- if (proposedVersion != remoteVersion) {
- // need to wait for another hello
- } else {
- // sent version is equal to remote --> version is negotiated
- version = proposedVersion;
- LOG.debug("version set: " + proposedVersion);
-
- // request features
- GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
- featuresBuilder.setVersion(version).setXid(hello.getXid());
- Future<RpcResult<GetFeaturesOutput>> featuresFuture = connectionAdapter
- .getFeatures(featuresBuilder.build());
- LOG.debug("waiting for features");
- RpcResult<GetFeaturesOutput> rpcFeatures;
- try {
- rpcFeatures = featuresFuture.get(getMaxTimeout(),
- TimeUnit.MILLISECONDS);
- if (!rpcFeatures.isSuccessful()) {
- LOG.error("obtained features problem: "
- + rpcFeatures.getErrors());
- } else {
- LOG.debug("obtained features: datapathId="
- + rpcFeatures.getResult().getDatapathId());
- conductorState = CONDUCTOR_STATE.WORKING;
-
- OFSessionUtil.registerSession(this,
- rpcFeatures.getResult(), version);
- LOG.info("handshake SETTLED");
- }
- } catch (Exception e) {
- handleException(e);
- }
- }
+ handshakeManager.setReceivedHello(hello);
+ hsPool.execute(handshakeManager);
}
/**
* @return rpc-response timeout in [ms]
*/
- private long getMaxTimeout() {
+ protected long getMaxTimeout() {
// TODO:: get from configuration
return 2000;
}
/**
- * @param e
+ * @return milliseconds
*/
- private void handleException(Exception e) {
- try {
- errorQueue.put(e);
- } catch (InterruptedException e1) {
- LOG.error(e1.getMessage(), e1);
- }
- }
-
- @Override
- public void onMultipartReplyMessage(MultipartReplyMessage arg0) {
- // TODO Auto-generated method stub
+ protected TimeUnit getMaxTimeoutUnit() {
+ // TODO:: get from configuration
+ return TimeUnit.MILLISECONDS;
}
@Override
- public void onMultipartRequestMessage(MultipartRequestMessage arg0) {
- // TODO Auto-generated method stub
+ public void onMultipartReplyMessage(MultipartReplyMessage message) {
+ queueKeeper.push(message, this);
}
@Override
- public void onPacketInMessage(PacketInMessage arg0) {
- // TODO Auto-generated method stub
+ public void onPacketInMessage(PacketInMessage message) {
+ queueKeeper.push(message, this);
}
@Override
- public void onPortStatusMessage(PortStatusMessage arg0) {
- // TODO Auto-generated method stub
+ public void onPortStatusMessage(PortStatusMessage message) {
+ this.getSessionContext().processPortStatusMsg(message);
+ queueKeeper.push(message, this);
}
@Override
public void onSwitchIdleEvent(SwitchIdleEvent notification) {
- if (!CONDUCTOR_STATE.WORKING.equals(conductorState)) {
- // idle state in any other conductorState than WORKING means real
- // problem and wont
- // be handled by echoReply
- // TODO: invalidate this connection + notify
- } else {
- LOG.debug("first idle state occured");
- EchoInputBuilder builder = new EchoInputBuilder();
- builder.setVersion(version);
- // TODO: get xid from sessionContext
- builder.setXid(42L);
-
- Future<RpcResult<EchoOutput>> echoReplyFuture = connectionAdapter
- .echo(builder.build());
-
- try {
- // TODO: read timeout from config
- RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(5,
- TimeUnit.SECONDS);
- if (echoReplyValue.isSuccessful()) {
- conductorState = CONDUCTOR_STATE.WORKING;
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ if (!CONDUCTOR_STATE.WORKING.equals(getConductorState())) {
+ // idle state in any other conductorState than WORKING means real
+ // problem and wont be handled by echoReply, but disconnection
+ disconnect();
+ OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
} else {
- for (RpcError replyError : echoReplyValue.getErrors()) {
- Throwable cause = replyError.getCause();
- LOG.error(
- "while receiving echoReply in TIMEOUTING state: "
- + cause.getMessage(), cause);
+ LOG.debug("first idle state occured");
+ EchoInputBuilder builder = new EchoInputBuilder();
+ builder.setVersion(getVersion());
+ builder.setXid(getSessionContext().getNextXid());
+
+ Future<RpcResult<EchoOutput>> echoReplyFuture = getConnectionAdapter()
+ .echo(builder.build());
+
+ try {
+ RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(getMaxTimeout(),
+ getMaxTimeoutUnit());
+ if (echoReplyValue.isSuccessful()) {
+ setConductorState(CONDUCTOR_STATE.WORKING);
+ } else {
+ for (RpcError replyError : echoReplyValue.getErrors()) {
+ Throwable cause = replyError.getCause();
+ LOG.error(
+ "while receiving echoReply in TIMEOUTING state: "
+ + cause.getMessage(), cause);
+ }
+ //switch issue occurred
+ throw new Exception("switch issue occurred");
+ }
+ } catch (Exception e) {
+ LOG.error("while waiting for echoReply in TIMEOUTING state: "
+ + e.getMessage(), e);
+ //switch is not responding
+ disconnect();
+ OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
}
}
- } catch (Exception e) {
- LOG.error("while waiting for echoReply in TIMEOUTING state: "
- + e.getMessage(), e);
}
- }
+
+ }).start();
}
/**
/**
* @param handshaking
*/
- private void checkState(CONDUCTOR_STATE expectedState) {
+ protected void checkState(CONDUCTOR_STATE expectedState) {
if (!conductorState.equals(expectedState)) {
throw new IllegalStateException("Expected state: " + expectedState
+ ", actual state:" + conductorState);
sessionManager.invalidateOnDisconnect(this);
}
- protected short proposeVersion(short remoteVersion) {
- Short proposal = null;
- for (short offer : versionOrder) {
- if (offer <= remoteVersion) {
- proposal = offer;
- break;
- }
- }
- if (proposal == null) {
- throw new IllegalArgumentException("unsupported version: "
- + remoteVersion);
- }
- return proposal;
- }
-
@Override
public Short getVersion() {
return version;
@Override
public Future<Boolean> disconnect() {
- return connectionAdapter.disconnect();
+ LOG.info("disconnecting: sessionCtx="+sessionContext+"|auxId="+auxiliaryKey);
+
+ Future<Boolean> result = null;
+ if (connectionAdapter.isAlive()) {
+ result = connectionAdapter.disconnect();
+ } else {
+ LOG.debug("connection already disconnected");
+ result = Futures.immediateFuture(true);
+ }
+
+ return result;
}
@Override
public SessionContext getSessionContext() {
return sessionContext;
}
+
+ @Override
+ public ConnectionAdapter getConnectionAdapter() {
+ return connectionAdapter;
+ }
+
+ @Override
+ public void onConnectionReady() {
+ LOG.debug("connection is ready-to-use");
+ hsPool.execute(handshakeManager);
+ }
+
+ @Override
+ public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
+ Short negotiatedVersion) {
+ version = negotiatedVersion;
+ conductorState = CONDUCTOR_STATE.WORKING;
+
+ OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
+ }
+
+ /**
+ * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
+ */
+ public void setBitmapNegotiationEnable(
+ boolean isBitmapNegotiationEnable) {
+ this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
+ }
+
+ protected void shutdownPool() {
+ hsPool.shutdownNow();
+ LOG.debug("pool is terminated: {}", hsPool.isTerminated());
+ }
}