import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.controller.sal.common.util.Futures;
import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
import org.opendaylight.openflowplugin.api.OFConstants;
import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.ElementsBuilder;
import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Futures;
/**
* testing handshake
*/
@RunWith(MockitoJUnitRunner.class)
public class HandshakeManagerImplTest {
-
+
private static final Logger LOG = LoggerFactory
.getLogger(HandshakeManagerImplTest.class);
-
+
private HandshakeManagerImpl handshakeManager;
@Mock
private ConnectionAdapter adapter;
private long helloXid = 42L;
private int expectedErrors = 0;
-
+
/**
* invoked before every test method
*/
handshakeManager.setErrorHandler(errorHandler);
handshakeManager.setHandshakeListener(handshakeListener);
handshakeManager.setUseVersionBitmap(false);
-
- resultFeatures = RpcResultsUtil.createRpcResult(true, new GetFeaturesOutputBuilder().build(), null);
-
+
+ resultFeatures = RpcResultBuilder.success(new GetFeaturesOutputBuilder().build()).build();
+
Mockito.when(adapter.hello(Matchers.any(HelloInput.class)))
- .thenReturn(Futures.immediateFuture(RpcResultsUtil.createRpcResult(true, (Void) null, null)));
+ .thenReturn(Futures.immediateFuture(
+ RpcResultBuilder.success((Void) null).build()));
}
-
+
/**
* invoked after each test method
*/
for (Throwable problem : errorCaptor.getAllValues()) {
LOG.warn(problem.getMessage(), problem);
}
-
+
Mockito.verify(errorHandler, Mockito.times(expectedErrors)).handleException(
Matchers.any(Throwable.class), Matchers.any(SessionContext.class));
}
{true, true, true, false, false, false},
{true, true, true, false, false}
};
-
+
for (Boolean[] verasionList : versions) {
ElementsBuilder elementsBuilder = new ElementsBuilder();
elementsBuilder.setVersionBitmap(Lists.newArrayList(verasionList));
}
//////// Version Negotiation Tests //////////////
-
+
/**
* Test of version negotiation Where switch version = 1.0
*
public void testVersionNegotiation10() throws Exception {
LOG.debug("testVersionNegotiation10");
Short version = OFConstants.OFP_VERSION_1_0;
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
}
-
+
/**
* Test of version negotiation Where switch version = 1.0
*
public void testVersionNegotiation10SwitchStarts() throws Exception {
LOG.debug("testVersionNegotiation10-ss");
Short version = OFConstants.OFP_VERSION_1_0;
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(resultFeatures.getResult(), version);
}
LOG.debug("testVersionNegotiation00");
expectedErrors = 1;
Short version = (short) 0x00;
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
}
-
+
/**
* Test of version negotiation Where switch version < 1.0
* Switch delivers first helloMessage with version 0x00 = negotiation unsuccessful
LOG.debug("testVersionNegotiation00-ss");
expectedErrors = 1;
Short version = (short) 0x00;
-
+
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
}
LOG.debug("testVersionNegotiation11");
Short version = (short) 0x02;
Short expVersion = (short) 0x01;
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), expVersion);
}
-
+
/**
* Test of version negotiation Where 1.0 < switch version < 1.3
*
LOG.debug("testVersionNegotiation11-ss");
Short version = (short) 0x02;
Short expVersion = (short) 0x01;
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), expVersion);
}
public void testVersionNegotiation13() throws Exception {
LOG.debug("testVersionNegotiation13");
Short version = OFConstants.OFP_VERSION_1_3;
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), version);
}
-
+
/**
* Test of version negotiation Where switch version = 1.3
*
public void testVersionNegotiation13SwitchStarts() throws Exception {
LOG.debug("testVersionNegotiation13-ss");
Short version = OFConstants.OFP_VERSION_1_3;
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), version);
}
LOG.debug("testVersionNegotiation15");
Short version = (short) 0x06;
Short expVersion = OFConstants.OFP_VERSION_1_3;
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), expVersion);
}
-
+
/**
* Test of version negotiation Where switch version >= 1.3
*
LOG.debug("testVersionNegotiation15-ss");
Short version = (short) 0x06;
Short expVersion = OFConstants.OFP_VERSION_1_3;
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(expVersion, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), expVersion);
}
LOG.debug("testVersionNegotiation15_MultipleCall");
Short version = (short) 0x06;
expectedErrors = 1;
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
}
-
+
/**
* Test of version negotiation Where switch version > 1.3
*
LOG.debug("testVersionNegotiation15_MultipleCall-ss");
Short version = (short) 0x06;
expectedErrors = 1;
-
+
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(createHelloMessage(version, helloXid).build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
}
HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.setReceivedHello(helloMessage.build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), version);
}
-
+
/**
* Test of version negotiation Where bitmap version {0x05,0x01}
*
HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_0), helloMessage);
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(helloMessage.build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), version);
}
HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.setReceivedHello(helloMessage.build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), version);
}
-
+
/**
* Test of version negotiation Where bitmap version {0x05,0x04}
*
HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
addVersionBitmap(Lists.newArrayList((short) 0x05, OFConstants.OFP_VERSION_1_3), helloMessage);
-
+
Mockito.when(adapter.getFeatures(Matchers.any(GetFeaturesInput.class)))
.thenReturn(Futures.immediateFuture(resultFeatures));
-
+
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(helloMessage.build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener).onHandshakeSuccessfull(
resultFeatures.getResult(), version);
}
Short version = (short) 0x05;
expectedErrors = 1;
handshakeManager.setUseVersionBitmap(true);
-
+
HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
-
+
handshakeManager.setReceivedHello(helloMessage.build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
}
-
+
/**
* Test of version negotiation Where bitmap version {0x05,0x02}
*
Short version = (short) 0x05;
expectedErrors = 1;
handshakeManager.setUseVersionBitmap(true);
-
+
HelloMessageBuilder helloMessage = createHelloMessage(version, helloXid);
addVersionBitmap(Lists.newArrayList((short) 0x05, (short) 0x02), helloMessage);
-
+
handshakeManager.shake();
-
+
handshakeManager.setReceivedHello(helloMessage.build());
handshakeManager.shake();
-
+
Mockito.verify(handshakeListener, Mockito.never()).onHandshakeSuccessfull(
Matchers.any(GetFeaturesOutput.class), Matchers.anyShort());
}
private static HelloMessageBuilder createHelloMessage(short ofpVersion10, long helloXid) {
return new HelloMessageBuilder().setVersion(ofpVersion10).setXid(helloXid);
}
-
+
/**
* @param versionOrder
* @param helloBuilder