Merge "RPC Manager Context Map holder change"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImplTest.java
index 94ef781bcad22c4c68eba75298c22381a11c4a4b..4427aeb7a95813b11a234b48a0e427107bfd0979 100644 (file)
@@ -17,14 +17,15 @@ import static org.mockito.Mockito.inOrder;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
-
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import io.netty.util.TimerTask;
 import java.lang.reflect.Field;
 import java.math.BigInteger;
 import java.util.Collections;
 import java.util.concurrent.ConcurrentHashMap;
-
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.FutureCallback;
+import java.util.concurrent.TimeUnit;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -49,38 +50,31 @@ import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
-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.common.types.rev130731.Capabilities;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 
 @RunWith(MockitoJUnitRunner.class)
 public class DeviceManagerImplTest {
 
-    private static final boolean TEST_VALUE_SWITCH_FEATURE_MANDATORY = true;
     private static final long TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA = 2000l;
-    private static final KeyedInstanceIdentifier<Node, NodeKey> DUMMY_NODE_II = InstanceIdentifier.create(Nodes.class)
-            .child(Node.class, new NodeKey(new NodeId("dummyNodeId")));
-    private static final Short DUMMY_TABLE_ID = 1;
-    private static final Long DUMMY_MAX_METER = 544L;
-    private static final String DUMMY_DATAPATH_ID = "44";
-    private static final Long DUMMY_PORT_NUMBER = 21L;
+    private static final int barrierCountLimit = 25600;
+    private static final int barrierIntervalNanos = 500;
+    public static final NodeId DUMMY_NODE_ID = new NodeId("dummyNodeId");
 
     @Mock
     CheckedFuture<Void, TransactionCommitFailedException> mockedFuture;
@@ -91,6 +85,8 @@ public class DeviceManagerImplTest {
     @Mock
     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
     @Mock
+    private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
+    @Mock
     private TranslatorLibrary translatorLibrary;
     @Mock
     private ConnectionContext mockConnectionContext;
@@ -100,30 +96,40 @@ public class DeviceManagerImplTest {
     private DeviceContextImpl mockedDeviceContext;
     @Mock
     private NodeId mockedNodeId;
+    @Mock
+    private LifecycleConductor lifecycleConductor;
+    @Mock
+    private MessageIntelligenceAgency messageIntelligenceAgency;
+    @Mock
+    private DeviceInfo deviceInfo;
 
     @Before
     public void setUp() throws Exception {
         OpenflowPortsUtil.init();
 
-        when(mockConnectionContext.getNodeId()).thenReturn(new NodeId("dummyNodeId"));
+        when(mockConnectionContext.getNodeId()).thenReturn(DUMMY_NODE_ID);
         when(mockConnectionContext.getFeatures()).thenReturn(mockFeatures);
         when(mockConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
+        when(mockConnectionContext.getDeviceInfo()).thenReturn(deviceInfo);
+        when(deviceInfo.getNodeId()).thenReturn(DUMMY_NODE_ID);
 
-        final Capabilities capabilitiesV13 = Mockito.mock(Capabilities.class);
-        final CapabilitiesV10 capabilitiesV10 = Mockito.mock(CapabilitiesV10.class);
+        final Capabilities capabilitiesV13 = mock(Capabilities.class);
+        final CapabilitiesV10 capabilitiesV10 = mock(CapabilitiesV10.class);
         when(mockFeatures.getCapabilities()).thenReturn(capabilitiesV13);
         when(mockFeatures.getCapabilitiesV10()).thenReturn(capabilitiesV10);
         when(mockFeatures.getDatapathId()).thenReturn(BigInteger.valueOf(21L));
+
+        when(lifecycleConductor.getMessageIntelligenceAgency()).thenReturn(messageIntelligenceAgency);
     }
 
-    @Test
-    public void onDeviceContextLevelUpFailTest() {
+    @Test(expected = IllegalStateException.class)
+    public void onDeviceContextLevelUpFailTest() throws Exception {
         onDeviceContextLevelUp(true);
     }
 
     @Test
-    public void onDeviceContextLevelUpSuccessTest() {
+    public void onDeviceContextLevelUpSuccessTest() throws Exception {
         onDeviceContextLevelUp(false);
     }
 
@@ -131,12 +137,12 @@ public class DeviceManagerImplTest {
         return prepareDeviceManager(false);
     }
 
-    private DeviceManagerImpl prepareDeviceManager(boolean withException) {
-        DataBroker mockedDataBroker = mock(DataBroker.class);
-        WriteTransaction mockedWriteTransaction = mock(WriteTransaction.class);
+    private DeviceManagerImpl prepareDeviceManager(final boolean withException) {
+        final DataBroker mockedDataBroker = mock(DataBroker.class);
+        final WriteTransaction mockedWriteTransaction = mock(WriteTransaction.class);
 
-        BindingTransactionChain mockedTxChain = mock(BindingTransactionChain.class);
-        WriteTransaction mockedWTx = mock(WriteTransaction.class);
+        final BindingTransactionChain mockedTxChain = mock(BindingTransactionChain.class);
+        final WriteTransaction mockedWTx = mock(WriteTransaction.class);
         when(mockedTxChain.newWriteOnlyTransaction()).thenReturn(mockedWTx);
         when(mockedDataBroker.createTransactionChain(any(TransactionChainListener.class))).thenReturn
                 (mockedTxChain);
@@ -144,25 +150,25 @@ public class DeviceManagerImplTest {
 
         when(mockedWriteTransaction.submit()).thenReturn(mockedFuture);
 
-        MessageIntelligenceAgency mockedMessageIntelligenceAgency = mock(MessageIntelligenceAgency.class);
-        DeviceManagerImpl deviceManager = new DeviceManagerImpl(mockedDataBroker, mockedMessageIntelligenceAgency,
-                TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA);
+        final DeviceManagerImpl deviceManager = new DeviceManagerImpl(mockedDataBroker,
+                TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA, false, barrierIntervalNanos, barrierCountLimit, lifecycleConductor);
+
         deviceManager.setDeviceInitializationPhaseHandler(deviceInitPhaseHandler);
+        deviceManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
 
         return deviceManager;
     }
 
-    public void onDeviceContextLevelUp(boolean withException) {
-        DeviceManagerImpl deviceManager = prepareDeviceManager(withException);
-        DeviceState mockedDeviceState = mock(DeviceState.class);
+    public void onDeviceContextLevelUp(final boolean withException) throws Exception {
+        final DeviceManagerImpl deviceManager = prepareDeviceManager(withException);
+        final DeviceState mockedDeviceState = mock(DeviceState.class);
         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
-        when(mockedDeviceState.getRole()).thenReturn(OfpRole.BECOMEMASTER);
 
         if (withException) {
             doThrow(new IllegalStateException("dummy")).when(mockedDeviceContext).initialSubmitTransaction();
         }
-
-        deviceManager.onDeviceContextLevelUp(mockedDeviceContext);
+        deviceManager.addDeviceContextToMap(DUMMY_NODE_ID, mockedDeviceContext);
+        deviceManager.onDeviceContextLevelUp(deviceInfo);
         if (withException) {
             verify(mockedDeviceContext).close();
         } else {
@@ -173,52 +179,75 @@ public class DeviceManagerImplTest {
 
     @Test
     public void deviceConnectedTest() throws Exception{
-        DeviceManagerImpl deviceManager = prepareDeviceManager();
+        final DeviceManagerImpl deviceManager = prepareDeviceManager();
         injectMockTranslatorLibrary(deviceManager);
-        ConnectionContext mockConnectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
+        final ConnectionContext mockConnectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
 
         deviceManager.deviceConnected(mockConnectionContext);
 
-        InOrder order = inOrder(mockConnectionContext);
+        final InOrder order = inOrder(mockConnectionContext);
         order.verify(mockConnectionContext).getFeatures();
         order.verify(mockConnectionContext).setOutboundQueueProvider(any(OutboundQueueProvider.class));
         order.verify(mockConnectionContext).setOutboundQueueHandleRegistration(
                 Mockito.<OutboundQueueHandlerRegistration<OutboundQueueProvider>>any());
         order.verify(mockConnectionContext).getNodeId();
-        Mockito.verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceContext>any());
+        verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceInfo>any());
     }
 
     @Test
     public void deviceConnectedV10Test() throws Exception{
-        DeviceManagerImpl deviceManager = prepareDeviceManager();
+        final DeviceManagerImpl deviceManager = prepareDeviceManager();
         injectMockTranslatorLibrary(deviceManager);
-        ConnectionContext mockConnectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_0);
+        final ConnectionContext mockConnectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_0);
 
-        PhyPortBuilder phyPort = new PhyPortBuilder()
+        final PhyPortBuilder phyPort = new PhyPortBuilder()
                 .setPortNo(41L);
         when(mockFeatures.getPhyPort()).thenReturn(Collections.singletonList(phyPort.build()));
-        MessageTranslator<Object, Object> mockedTranslator = Mockito.mock(MessageTranslator.class);
-        when(mockedTranslator.translate(Matchers.<Object>any(), Matchers.<DeviceContext>any(), Matchers.any()))
+        final MessageTranslator<Object, Object> mockedTranslator = mock(MessageTranslator.class);
+        when(mockedTranslator.translate(Matchers.<Object>any(), Matchers.<DeviceState>any(), Matchers.any()))
                 .thenReturn(null);
         when(translatorLibrary.lookupTranslator(Matchers.<TranslatorKey>any())).thenReturn(mockedTranslator);
 
         deviceManager.deviceConnected(mockConnectionContext);
 
-        InOrder order = inOrder(mockConnectionContext);
+        final InOrder order = inOrder(mockConnectionContext);
         order.verify(mockConnectionContext).getFeatures();
         order.verify(mockConnectionContext).setOutboundQueueProvider(any(OutboundQueueProvider.class));
         order.verify(mockConnectionContext).setOutboundQueueHandleRegistration(
                 Mockito.<OutboundQueueHandlerRegistration<OutboundQueueProvider>>any());
         order.verify(mockConnectionContext).getNodeId();
-        Mockito.verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceContext>any());
+        verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceInfo>any());
+    }
+
+    @Test
+    public void deviceDisconnectedTest() throws Exception {
+        final DeviceState deviceState = mock(DeviceState.class);
+
+        final DeviceManagerImpl deviceManager = prepareDeviceManager();
+        injectMockTranslatorLibrary(deviceManager);
+
+        final ConnectionContext connectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
+        when(connectionContext.getNodeId()).thenReturn(mockedNodeId);
+
+        final DeviceContext deviceContext = mock(DeviceContext.class);
+        when(deviceContext.shuttingDownDataStoreTransactions()).thenReturn(Futures.immediateCheckedFuture(null));
+        when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+        when(deviceContext.getDeviceState()).thenReturn(deviceState);
+
+        final ConcurrentHashMap<NodeId, DeviceContext> deviceContexts = getContextsCollection(deviceManager);
+        deviceContexts.put(DUMMY_NODE_ID, deviceContext);
+
+        deviceManager.onDeviceDisconnected(connectionContext);
+
+        verify(lifecycleConductor).newTimeout(Mockito.<TimerTask>any(), Mockito.anyLong(), Mockito.<TimeUnit>any());
     }
 
-    protected ConnectionContext buildMockConnectionContext(short ofpVersion) {
+    protected ConnectionContext buildMockConnectionContext(final short ofpVersion) {
         when(mockFeatures.getVersion()).thenReturn(ofpVersion);
         when(outboundQueueProvider.reserveEntry()).thenReturn(43L);
         Mockito.doAnswer(new Answer<Void>() {
             @Override
-            public Void answer(InvocationOnMock invocation) throws Throwable {
+            public Void answer(final InvocationOnMock invocation) throws Throwable {
                 final FutureCallback<OfHeader> callBack = (FutureCallback<OfHeader>) invocation.getArguments()[2];
                 callBack.onSuccess(null);
                 return null;
@@ -230,8 +259,8 @@ public class DeviceManagerImplTest {
         when(mockedConnectionAdapter.registerOutboundQueueHandler(Matchers.<OutboundQueueHandler>any(), Matchers.anyInt(), Matchers.anyLong()))
                 .thenAnswer(new Answer<OutboundQueueHandlerRegistration<OutboundQueueHandler>>() {
                     @Override
-                    public OutboundQueueHandlerRegistration<OutboundQueueHandler> answer(InvocationOnMock invocation) throws Throwable {
-                        OutboundQueueHandler handler = (OutboundQueueHandler) invocation.getArguments()[0];
+                    public OutboundQueueHandlerRegistration<OutboundQueueHandler> answer(final InvocationOnMock invocation) throws Throwable {
+                        final OutboundQueueHandler handler = (OutboundQueueHandler) invocation.getArguments()[0];
                         handler.onConnectionQueueChanged(outboundQueueProvider);
                         return null;
                     }
@@ -241,13 +270,13 @@ public class DeviceManagerImplTest {
         return mockConnectionContext;
     }
 
-    private void injectMockTranslatorLibrary(DeviceManagerImpl deviceManager) {
+    private void injectMockTranslatorLibrary(final DeviceManagerImpl deviceManager) {
         deviceManager.setTranslatorLibrary(translatorLibrary);
     }
 
     @Test
     public void testClose() throws Exception {
-        DeviceContext deviceContext = Mockito.mock(DeviceContext.class);
+        final DeviceContext deviceContext = mock(DeviceContext.class);
         final DeviceManagerImpl deviceManager = prepareDeviceManager();
         final ConcurrentHashMap<NodeId, DeviceContext> deviceContexts = getContextsCollection(deviceManager);
         deviceContexts.put(mockedNodeId, deviceContext);
@@ -255,10 +284,11 @@ public class DeviceManagerImplTest {
 
         deviceManager.close();
 
-        Mockito.verify(deviceContext).close();
+        verify(deviceContext).shutdownConnection();
+        verify(deviceContext, Mockito.never()).close();
     }
 
-    private static ConcurrentHashMap<NodeId, DeviceContext> getContextsCollection(DeviceManagerImpl deviceManager) throws NoSuchFieldException, IllegalAccessException {
+    private static ConcurrentHashMap<NodeId, DeviceContext> getContextsCollection(final DeviceManagerImpl deviceManager) throws NoSuchFieldException, IllegalAccessException {
         // HACK: contexts collection for testing shall be accessed in some more civilized way
         final Field contextsField = DeviceManagerImpl.class.getDeclaredField("deviceContexts");
         Assert.assertNotNull(contextsField);