X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Flifecycle%2FContextChainHolderImplTest.java;h=a4b80f653e71f5fbcde8bc3bd32d1ff5fd62b322;hb=dfadca522536e6cfe7a9eba0255b0cd483d68b66;hp=59c670d40bcb4777c529ed7d05d21d6ad164e795;hpb=ea70a64f299108bffdd762793114f362095ec46b;p=openflowplugin.git diff --git a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/lifecycle/ContextChainHolderImplTest.java b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/lifecycle/ContextChainHolderImplTest.java index 59c670d40b..a4b80f653e 100644 --- a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/lifecycle/ContextChainHolderImplTest.java +++ b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/lifecycle/ContextChainHolderImplTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved. * * This program and the accompanying materials are made available under the @@ -8,29 +8,49 @@ package org.opendaylight.openflowplugin.impl.lifecycle; import com.google.common.util.concurrent.Futures; -import io.netty.util.HashedWheelTimer; +import java.util.concurrent.ExecutorService; 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.runners.MockitoJUnitRunner; -import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; +import org.mockito.junit.MockitoJUnitRunner; +import org.opendaylight.mdsal.eos.binding.api.Entity; +import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService; +import org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange; +import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider; import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext; +import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus; import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext; import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo; import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager; +import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState; +import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager; +import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent; +import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration; +import org.opendaylight.openflowplugin.api.openflow.role.RoleContext; +import org.opendaylight.openflowplugin.api.openflow.role.RoleManager; import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext; import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager; import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext; import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager; +import org.opendaylight.openflowplugin.impl.mastership.MastershipChangeServiceManagerImpl; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState; +import org.opendaylight.yangtools.concepts.Registration; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint8; @RunWith(MockitoJUnitRunner.class) public class ContextChainHolderImplTest { - @Mock - private HashedWheelTimer timer; + private static final String ENTITY_TEST = "EntityTest"; + private static final String OPENFLOW_TEST = "openflow:test"; + private static final Uint8 AUXILIARY_ID = Uint8.ZERO; + private static final Uint32 DEVICE_DATASTORE_REMOVAL_DELAY = Uint32.valueOf(500); @Mock private StatisticsManager statisticsManager; @Mock @@ -38,88 +58,222 @@ public class ContextChainHolderImplTest { @Mock private DeviceManager deviceManager; @Mock + private RoleManager roleManager; + @Mock private StatisticsContext statisticsContext; @Mock private RpcContext rpcContext; @Mock private DeviceContext deviceContext; @Mock + private RoleContext roleContext; + @Mock private ConnectionContext connectionContext; @Mock private DeviceInfo deviceInfo; @Mock private ClusterSingletonServiceProvider singletonServicesProvider; + @Mock + private ExecutorService executorService; + @Mock + private Registration clusterSingletonServiceRegistration; + @Mock + private EntityOwnershipService entityOwnershipService; + @Mock + private Registration entityOwnershipListenerRegistration; + @Mock + private ReconciliationFrameworkEvent reconciliationFrameworkEvent; + @Mock + private FeaturesReply featuresReply; + @Mock + private OpenflowProviderConfig config; private ContextChainHolderImpl contextChainHolder; + private ReconciliationFrameworkRegistration registration; + private final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl(); @Before public void setUp() throws Exception { - contextChainHolder = new ContextChainHolderImpl(timer); + Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo); + Mockito.when(deviceManager.createContext(connectionContext)).thenReturn(deviceContext); + Mockito.when(rpcManager.createContext(deviceContext)).thenReturn(rpcContext); + Mockito.when(roleManager.createContext(deviceContext)).thenReturn(roleContext); + Mockito.when(statisticsManager.createContext(Mockito.eq(deviceContext), Mockito.anyBoolean())) + .thenReturn(statisticsContext); + Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo); + + Mockito.when(singletonServicesProvider.registerClusterSingletonService(Mockito.any())) + .thenReturn(clusterSingletonServiceRegistration); + Mockito.when(entityOwnershipService.registerListener(Mockito.any(), Mockito.any())) + .thenReturn(entityOwnershipListenerRegistration); + Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply); + Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID); + Mockito.when(config.getDeviceDatastoreRemovalDelay()) + .thenReturn(new NonZeroUint32Type(DEVICE_DATASTORE_REMOVAL_DELAY)); + + registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent); + + contextChainHolder = new ContextChainHolderImpl( + executorService, + singletonServicesProvider, + entityOwnershipService, + manager, + config); contextChainHolder.addManager(statisticsManager); contextChainHolder.addManager(rpcManager); contextChainHolder.addManager(deviceManager); - contextChainHolder.addSingletonServicesProvider(singletonServicesProvider); - Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo); - Mockito.when(deviceManager.createContext(connectionContext)).thenReturn(deviceContext); - Mockito.when(rpcManager.createContext( - deviceInfo, - deviceContext)) - .thenReturn(rpcContext); - Mockito.when(statisticsManager.createContext(deviceContext)).thenReturn(statisticsContext); - Mockito.when(deviceContext.makeDeviceSlave()).thenReturn(Futures.immediateFuture(null)); + contextChainHolder.addManager(roleManager); } @Test - public void addManager() throws Exception { + public void addManager() { Assert.assertTrue(contextChainHolder.checkAllManagers()); } @Test - public void createContextChain() throws Exception { + public void createContextChain() { contextChainHolder.createContextChain(connectionContext); Mockito.verify(deviceManager).createContext(Mockito.any(ConnectionContext.class)); - Mockito.verify(rpcManager).createContext(Mockito.any(DeviceInfo.class), Mockito.any(DeviceContext.class)); - Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class)); + Mockito.verify(rpcManager).createContext(Mockito.any(DeviceContext.class)); + Mockito.verify(roleManager).createContext(Mockito.any(DeviceContext.class)); + Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class), Mockito.anyBoolean()); } - @Test - public void destroyContextChain() throws Exception { + @Test + public void reconciliationFrameworkFailure() { + Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)) + .thenReturn(Futures.immediateFailedFuture(new Throwable("test"))); + contextChainHolder.createContextChain(connectionContext); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE); + Mockito.verify(connectionContext).closeConnection(false); } @Test - public void pairConnection() throws Exception { - + public void reconciliationFrameworkDisconnect() { + Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)) + .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT)); + contextChainHolder.createContextChain(connectionContext); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE); + Mockito.verify(connectionContext).closeConnection(false); } @Test - public void deviceConnected() throws Exception { - + public void reconciliationFrameworkSuccess() { + contextChainHolder.createContextChain(connectionContext); + Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)) + .thenReturn(Futures.immediateFuture(ResultState.DONOTHING)); + contextChainHolder.createContextChain(connectionContext); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE); + Mockito.verify(reconciliationFrameworkEvent).onDevicePrepared(deviceInfo); } @Test - public void onNotAbleToStartMastership() throws Exception { - + public void reconciliationFrameworkSuccessButNotSubmit() { + contextChainHolder.createContextChain(connectionContext); + Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)) + .thenReturn(Futures.immediateFuture(ResultState.DONOTHING)); + // TODO when if (future != null) check in MastershipChangeServiceManagerImpl's becomeSlaveOrDisconnect() is rm + // Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)) + // .thenReturn(Futures.immediateFuture(null)); + contextChainHolder.createContextChain(connectionContext); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE); + contextChainHolder.onNotAbleToStartMastershipMandatory(deviceInfo, "Test reason"); + Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo); + Mockito.verify(connectionContext).closeConnection(false); } @Test - public void onMasterRoleAcquired() throws Exception { - + public void deviceMastered() throws Exception { + registration.close(); + contextChainHolder.createContextChain(connectionContext); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION); + Assert.assertFalse(contextChainHolder.isAnyDeviceMastered()); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE); + Assert.assertFalse(contextChainHolder.isAnyDeviceMastered()); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT); + Assert.assertTrue(contextChainHolder.isAnyDeviceMastered()); + Assert.assertTrue(contextChainHolder.listOfMasteredDevices().size() == 1); } @Test - public void onSlaveRoleAcquired() throws Exception { + public void deviceConnected() throws Exception { + registration.close(); + Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext) + == ConnectionStatus.MAY_CONTINUE); + Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(Uint8.ONE); + Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext) + == ConnectionStatus.MAY_CONTINUE); + Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID); + Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext) + == ConnectionStatus.MAY_CONTINUE); + } + @Test + public void notToAbleMastership() throws Exception { + registration.close(); + contextChainHolder.deviceConnected(connectionContext); + contextChainHolder.onNotAbleToStartMastership(deviceInfo, "Test reason", true); + Mockito.verify(deviceContext).close(); + Mockito.verify(statisticsContext).close(); + Mockito.verify(rpcContext).close(); } @Test - public void onSlaveRoleNotAcquired() throws Exception { + public void notAbleToSetSlave() throws Exception { + registration.close(); + contextChainHolder.deviceConnected(connectionContext); + contextChainHolder.onSlaveRoleNotAcquired(deviceInfo, "Test reason"); + Mockito.verify(deviceContext).close(); + Mockito.verify(statisticsContext).close(); + Mockito.verify(rpcContext).close(); + } + @Test + public void deviceDisconnected() throws Exception { + registration.close(); + contextChainHolder.createContextChain(connectionContext); + contextChainHolder.onDeviceDisconnected(connectionContext); + Mockito.verify(deviceContext).close(); + Mockito.verify(statisticsContext).close(); + Mockito.verify(rpcContext).close(); } @Test - public void onDeviceDisconnected() throws Exception { + public void onClose() throws Exception { + registration.close(); + contextChainHolder.createContextChain(connectionContext); + contextChainHolder.close(); + Mockito.verify(deviceContext).close(); + Mockito.verify(statisticsContext).close(); + Mockito.verify(rpcContext).close(); + } + @Test + public void ownershipChanged() throws Exception { + registration.close(); + contextChainHolder.createContextChain(connectionContext); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT); + contextChainHolder.ownershipChanged(new Entity(ENTITY_TEST, OPENFLOW_TEST), + EntityOwnershipStateChange.LOCAL_OWNERSHIP_LOST_NO_OWNER, false); + Mockito.verify(deviceManager, Mockito.timeout(1000)).removeDeviceFromOperationalDS(Mockito.any()); } -} \ No newline at end of file + @Test + public void ownershipChangedButHasOwner() throws Exception { + registration.close(); + contextChainHolder.createContextChain(connectionContext); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE); + contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT); + contextChainHolder.ownershipChanged(new Entity(ENTITY_TEST, OPENFLOW_TEST), + EntityOwnershipStateChange.LOCAL_OWNERSHIP_LOST_NEW_OWNER, false); + Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any()); + } +}