package org.opendaylight.openflowplugin.impl.role;
+import com.google.common.util.concurrent.Futures;
+import io.netty.util.HashedWheelTimer;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
-import org.mockito.stubbing.Answer;
import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
+import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
-/**
- * @author Jozef Bacigal
- * Date: 4/19/16
- * Time: 12:56
- */
@RunWith(MockitoJUnitRunner.class)
public class RoleContextImplTest {
- private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
-
@Mock
- private EntityOwnershipService entityOwnershipService;
-
+ HashedWheelTimer hashedWheelTimer;
@Mock
- private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
-
+ private DeviceInfo deviceInfo;
+ @Mock
+ private RoleManager roleManager;
@Mock
- private LifecycleConductor conductor;
+ private LifecycleService lifecycleService;
+ @Mock
+ private SalRoleService salRoleService;
private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
- private final Entity entity = new Entity(RoleManager.ENTITY_TYPE, nodeId.getValue());
- private final Entity txEntity = new Entity(RoleManager.TX_ENTITY_TYPE, nodeId.getValue());
private RoleContext roleContext;
+ private RoleContextImpl roleContextSpy;
@Before
public void setup() throws CandidateAlreadyRegisteredException {
- roleContext = new RoleContextImpl(nodeId, entityOwnershipService, entity, txEntity, conductor);
- Mockito.when(entityOwnershipService.registerCandidate(entity)).thenReturn(entityOwnershipCandidateRegistration);
- Mockito.when(entityOwnershipService.registerCandidate(txEntity)).thenReturn(entityOwnershipCandidateRegistration);
+ roleContext = new RoleContextImpl(deviceInfo, hashedWheelTimer, roleManager, lifecycleService);
+ roleContext.setSalRoleService(salRoleService);
+ Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
+ Mockito.when(salRoleService.setRole(Mockito.<SetRoleInput>any())).thenReturn(Futures.immediateFuture(null));
+ Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(DeviceStateUtil.createNodeInstanceIdentifier(nodeId));
+ roleContextSpy = Mockito.spy((RoleContextImpl) roleContext);
}
-// @Test
-// Run this test only if demanded because it takes 15s to run
- public void testInitializationThreads() throws Exception {
-
- /*Setting answer which will hold the answer for 5s*/
- Mockito.when(entityOwnershipService.registerCandidate(entity)).thenAnswer(new Answer<EntityOwnershipService>() {
- @Override
- public EntityOwnershipService answer(final InvocationOnMock invocationOnMock) throws Throwable {
- LOG.info("Sleeping this thread for 14s");
- Thread.sleep(14000L);
- return null;
- }
- });
-
- Thread t1 = new Thread(new Runnable() {
- @Override
- public void run() {
- LOG.info("Starting thread 1");
- Assert.assertTrue(roleContext.initialization());
- }
- });
-
- Thread t2 = new Thread(new Runnable() {
- @Override
- public void run() {
- LOG.info("Starting thread 2");
- Assert.assertFalse(roleContext.initialization());
- }
- });
-
- t1.start();
- LOG.info("Sleeping main thread for 1s to prevent race condition.");
- Thread.sleep(1000L);
- t2.start();
-
- while (t2.isAlive()) {
- //Waiting
- }
+ @Test
+ public void testCreateRequestContext() throws Exception {
+ roleContext.createRequestContext();
+ Mockito.verify(deviceInfo).reserveXidForDeviceMessage();
+ }
+ @Test(expected = NullPointerException.class)
+ public void testSetSalRoleService() throws Exception {
+ roleContext.setSalRoleService(null);
}
@Test
- public void testTermination() throws Exception {
- roleContext.registerCandidate(entity);
- roleContext.registerCandidate(txEntity);
- Assert.assertTrue(roleContext.isMainCandidateRegistered());
- Assert.assertTrue(roleContext.isTxCandidateRegistered());
- roleContext.unregisterAllCandidates();
- Assert.assertFalse(roleContext.isMainCandidateRegistered());
+ public void testGetNodeId() throws Exception {
+ Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
}
@Test
- public void testCreateRequestContext() throws Exception {
-
+ public void startupClusterServices() throws Exception {
+ roleContextSpy.startupClusterServices();
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
}
- @Test(expected = NullPointerException.class)
- public void testSetSalRoleService() throws Exception {
- roleContext.setSalRoleService(null);
+ @Test
+ public void startupClusterServicesVersion10() throws Exception {
+ Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
+ roleContextSpy.startupClusterServices();
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
}
@Test
- public void testGetEntity() throws Exception {
- Assert.assertTrue(roleContext.getEntity().equals(entity));
+ public void startupClusterServicesVersion13() throws Exception {
+ Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
+ roleContextSpy.startupClusterServices();
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
}
@Test
- public void testGetTxEntity() throws Exception {
- Assert.assertTrue(roleContext.getTxEntity().equals(txEntity));
+ public void stopClusterServicesNotDisconnected() throws Exception {
+ roleContextSpy.stopClusterServices(false);
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
+ Mockito.verify(roleManager, Mockito.never()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
}
@Test
- public void testGetNodeId() throws Exception {
- Assert.assertTrue(roleContext.getNodeId().equals(nodeId));
+ public void stopClusterServicesDisconnected() throws Exception {
+ roleContextSpy.stopClusterServices(true);
+ Mockito.verify(roleManager, Mockito.atLeastOnce()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
}
@Test
- public void testIsMaster() throws Exception {
- Assert.assertTrue(roleContext.initialization());
- Assert.assertFalse(roleContext.isMaster());
- Assert.assertTrue(roleContext.registerCandidate(txEntity));
- Assert.assertTrue(roleContext.isMaster());
- Assert.assertTrue(roleContext.unregisterCandidate(entity));
- Assert.assertFalse(roleContext.isMaster());
+ public void makeDeviceSlave() throws Exception {
+ roleContextSpy.makeDeviceSlave();
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
}
+
}