*/
package org.opendaylight.openflowplugin.impl.role;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import com.google.common.util.concurrent.SettableFuture;
+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.mockito.ArgumentMatcher;
-import org.mockito.Matchers;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
-import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
import org.opendaylight.openflowplugin.api.OFConstants;
-import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
-import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
-import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
+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.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
-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.GetFeaturesOutput;
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;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
-import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
-/**
- * Created by kramesha on 9/1/15.
- */
+@RunWith(MockitoJUnitRunner.class)
public class RoleContextImplTest {
@Mock
- private EntityOwnershipService entityOwnershipService;
-
+ HashedWheelTimer hashedWheelTimer;
@Mock
- private DataBroker dataBroker;
-
+ private DeviceInfo deviceInfo;
@Mock
- private RpcProviderRegistry rpcProviderRegistry;
-
+ private RoleManager roleManager;
@Mock
- private DeviceContext deviceContext;
-
- @Mock
- private ConnectionContext connectionContext;
-
- @Mock
- private DeviceState deviceState;
-
+ private LifecycleService lifecycleService;
@Mock
private SalRoleService salRoleService;
- @Mock
- private GetFeaturesOutput getFeaturesOutput;
-
- @Mock
- private FeaturesReply featuresReply;
-
private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
- private final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
- 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() {
- MockitoAnnotations.initMocks(this);
- when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
- when(deviceContext.getDeviceState()).thenReturn(deviceState);
- when(connectionContext.getNodeId()).thenReturn(nodeId);
- when(deviceState.getNodeInstanceIdentifier()).thenReturn(instanceIdentifier);
- when(rpcProviderRegistry.getRpcService(SalRoleService.class)).thenReturn(salRoleService);
- when(deviceState.getFeatures()).thenReturn(getFeaturesOutput);
- when(getFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
- when(deviceContext.getPrimaryConnectionContext().getFeatures()).thenReturn(featuresReply);
- when(deviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
+ public void setup() throws CandidateAlreadyRegisteredException {
+ roleContext = new RoleContextImpl(deviceInfo, hashedWheelTimer, roleManager);
+ roleContext.setSalRoleService(salRoleService);
+ Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
+ Mockito.when(salRoleService.setRole(Mockito.<SetRoleInput>any())).thenReturn(Futures.immediateFuture(null));
+ roleContextSpy = Mockito.spy((RoleContextImpl) roleContext);
}
@Test
- public void testOnRoleChanged() {
- final OfpRole newRole = OfpRole.BECOMEMASTER;
-
- final SettableFuture<RpcResult<SetRoleOutput>> future = SettableFuture.create();
- future.set(RpcResultBuilder.<SetRoleOutput>success().build());
- when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier))))
- .thenReturn(future);
-
- final RoleContextImpl roleContext = new RoleContextImpl(deviceContext, entityOwnershipService, entity, txEntity);
- roleContext.setSalRoleService(salRoleService);
+ public void testCreateRequestContext() throws Exception {
+ roleContext.createRequestContext();
+ Mockito.verify(deviceInfo).reserveXidForDeviceMessage();
+ }
- roleContext.onRoleChanged(OfpRole.BECOMESLAVE, newRole);
+ @Test(expected = NullPointerException.class)
+ public void testSetSalRoleService() throws Exception {
+ roleContext.setSalRoleService(null);
+ }
- verify(deviceState).setRole(newRole);
+ @Test
+ public void testGetNodeId() throws Exception {
+ Assert.assertTrue(roleContext.getDeviceInfo().getNodeId().equals(nodeId));
}
+ @Test
+ public void startupClusterServices() throws Exception {
+ Mockito.when(deviceInfo.getVersion()).thenReturn(null);
+ roleContextSpy.startupClusterServices();
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
+ }
- private class SetRoleInputMatcher extends ArgumentMatcher<SetRoleInput> {
+ @Test
+ public void startupClusterServicesVersion10() throws Exception {
+ Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
+ roleContextSpy.startupClusterServices();
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
+ }
- private final OfpRole ofpRole;
- private final NodeRef nodeRef;
+ @Test
+ public void startupClusterServicesVersion13() throws Exception {
+ Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
+ roleContextSpy.startupClusterServices();
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMEMASTER);
+ }
- public SetRoleInputMatcher(final OfpRole ofpRole, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
- this.ofpRole = ofpRole;
- nodeRef = new NodeRef(instanceIdentifier);
+ @Test
+ public void stopClusterServicesNotDisconnected() throws Exception {
+ roleContextSpy.stopClusterServices(false);
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
+ Mockito.verify(roleManager, Mockito.never()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any(), Mockito.anyInt());
+ }
- }
+ @Test
+ public void stopClusterServicesDisconnected() throws Exception {
+ roleContextSpy.stopClusterServices(true);
+ Mockito.verify(roleManager, Mockito.atLeastOnce()).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any(), Mockito.anyInt());
+ }
- @Override
- public boolean matches(final Object o) {
- final SetRoleInput input = (SetRoleInput) o;
- if (input.getControllerRole() == ofpRole &&
- input.getNode().equals(nodeRef)) {
- return true;
- }
- return false;
- }
+ @Test
+ public void makeDeviceSlave() throws Exception {
+ roleContextSpy.makeDeviceSlave();
+ Mockito.verify(roleContextSpy).sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
}
+
}