Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImplTest.java
index e57c4a5d842cd8a386bdb2b1a2d7e66babd30b7f..17abab3ea32242b59b98c6f34a0415a90a985a79 100644 (file)
@@ -1,12 +1,9 @@
 /*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
- *  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
- *  *
- *  * This program and the accompanying materials are made available under the
- *  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- *  * and is available at http://www.eclipse.org/legal/epl-v10.html
- *
- *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
 package org.opendaylight.openflowplugin.impl.device;
@@ -21,12 +18,11 @@ 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 io.netty.util.HashedWheelTimer;
 import java.lang.reflect.Field;
 import java.math.BigInteger;
 import java.util.Collections;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.TimeUnit;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -41,13 +37,17 @@ import org.mockito.stubbing.Answer;
 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandler;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
 import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.OFPContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
@@ -57,17 +57,20 @@ 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.lifecycle.LifecycleService;
 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.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+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.yangtools.yang.binding.KeyedInstanceIdentifier;
 
 @RunWith(MockitoJUnitRunner.class)
 public class DeviceManagerImplTest {
@@ -98,16 +101,23 @@ public class DeviceManagerImplTest {
     @Mock
     private NodeId mockedNodeId;
     @Mock
-    private LifecycleConductor lifecycleConductor;
-    @Mock
     private MessageIntelligenceAgency messageIntelligenceAgency;
     @Mock
     private DeviceInfo deviceInfo;
-
+    @Mock
+    private LifecycleService lifecycleService;
+    @Mock
+    private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
+    @Mock
+    private EntityOwnershipService entityOwnershipService;
+    @Mock
+    private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
+    @Mock
+    private ConvertorExecutor convertorExecutor;
+    @Mock
+    private KeyedInstanceIdentifier<Node, NodeKey> key;
     @Before
     public void setUp() throws Exception {
-        OpenflowPortsUtil.init();
-
         when(mockConnectionContext.getNodeId()).thenReturn(DUMMY_NODE_ID);
         when(mockConnectionContext.getFeatures()).thenReturn(mockFeatures);
         when(mockConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
@@ -120,13 +130,6 @@ public class DeviceManagerImplTest {
         when(mockFeatures.getCapabilities()).thenReturn(capabilitiesV13);
         when(mockFeatures.getCapabilitiesV10()).thenReturn(capabilitiesV10);
         when(mockFeatures.getDatapathId()).thenReturn(BigInteger.valueOf(21L));
-
-        when(lifecycleConductor.getMessageIntelligenceAgency()).thenReturn(messageIntelligenceAgency);
-    }
-
-    @Test(expected = IllegalStateException.class)
-    public void onDeviceContextLevelUpFailTest() throws Exception {
-        onDeviceContextLevelUp(true);
     }
 
     @Test
@@ -135,10 +138,6 @@ public class DeviceManagerImplTest {
     }
 
     private DeviceManagerImpl prepareDeviceManager() {
-        return prepareDeviceManager(false);
-    }
-
-    private DeviceManagerImpl prepareDeviceManager(final boolean withException) {
         final DataBroker mockedDataBroker = mock(DataBroker.class);
         final WriteTransaction mockedWriteTransaction = mock(WriteTransaction.class);
 
@@ -150,9 +149,19 @@ public class DeviceManagerImplTest {
         when(mockedDataBroker.newWriteOnlyTransaction()).thenReturn(mockedWriteTransaction);
 
         when(mockedWriteTransaction.submit()).thenReturn(mockedFuture);
-
-        final DeviceManagerImpl deviceManager = new DeviceManagerImpl(mockedDataBroker,
-                TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA, false, barrierIntervalNanos, barrierCountLimit, lifecycleConductor, true);
+        when(entityOwnershipService.registerListener(any(), any())).thenReturn(entityOwnershipListenerRegistration);
+
+        final DeviceManagerImpl deviceManager = new DeviceManagerImpl(
+                mockedDataBroker,
+                TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA,
+                false,
+                barrierIntervalNanos,
+                barrierCountLimit,
+                messageIntelligenceAgency,
+                true,
+                clusterSingletonServiceProvider,
+                entityOwnershipService, new HashedWheelTimer(), convertorExecutor, false, null
+        );
 
         deviceManager.setDeviceInitializationPhaseHandler(deviceInitPhaseHandler);
         deviceManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
@@ -161,7 +170,7 @@ public class DeviceManagerImplTest {
     }
 
     public void onDeviceContextLevelUp(final boolean withException) throws Exception {
-        final DeviceManagerImpl deviceManager = prepareDeviceManager(withException);
+        final DeviceManagerImpl deviceManager = prepareDeviceManager();
         final DeviceState mockedDeviceState = mock(DeviceState.class);
         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
 
@@ -169,11 +178,10 @@ public class DeviceManagerImplTest {
             doThrow(new IllegalStateException("dummy")).when(mockedDeviceContext).initialSubmitTransaction();
         }
         deviceManager.addDeviceContextToMap(deviceInfo, mockedDeviceContext);
-        deviceManager.onDeviceContextLevelUp(deviceInfo);
+        deviceManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
         if (withException) {
             verify(mockedDeviceContext).close();
         } else {
-            verify(mockedDeviceContext).initialSubmitTransaction();
             verify(mockedDeviceContext).onPublished();
         }
     }
@@ -190,7 +198,7 @@ public class DeviceManagerImplTest {
         order.verify(mockConnectionContext).setOutboundQueueProvider(any(OutboundQueueProvider.class));
         order.verify(mockConnectionContext).setOutboundQueueHandleRegistration(
                 Mockito.<OutboundQueueHandlerRegistration<OutboundQueueProvider>>any());
-        verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceInfo>any());
+        verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceInfo>any(), Mockito.<LifecycleService>any());
     }
 
     @Test
@@ -213,7 +221,7 @@ public class DeviceManagerImplTest {
         order.verify(mockConnectionContext).setOutboundQueueProvider(any(OutboundQueueProvider.class));
         order.verify(mockConnectionContext).setOutboundQueueHandleRegistration(
                 Mockito.<OutboundQueueHandlerRegistration<OutboundQueueProvider>>any());
-        verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceInfo>any());
+        verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceInfo>any(), Mockito.<LifecycleService>any());
     }
 
     @Test
@@ -230,13 +238,12 @@ public class DeviceManagerImplTest {
         when(deviceContext.shuttingDownDataStoreTransactions()).thenReturn(Futures.immediateCheckedFuture(null));
         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
         when(deviceContext.getDeviceState()).thenReturn(deviceState);
+        when(deviceContext.getState()).thenReturn(OFPContext.CONTEXT_STATE.WORKING);
 
         final ConcurrentHashMap<DeviceInfo, DeviceContext> deviceContexts = getContextsCollection(deviceManager);
         deviceContexts.put(deviceInfo, deviceContext);
 
         deviceManager.onDeviceDisconnected(connectionContext);
-
-        verify(lifecycleConductor).newTimeout(Mockito.<TimerTask>any(), Mockito.anyLong(), Mockito.<TimeUnit>any());
     }
 
     protected ConnectionContext buildMockConnectionContext(final short ofpVersion) {