Bug 5596 Cleaning part 1
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / LifecycleConductorImplTest.java
index 63c28cdb6349a2d60513a7602fe119c8f39cfda4..723cb79723a2cb5ee74f1ba8a83f4ce2bbb66456 100644 (file)
@@ -9,32 +9,46 @@ package org.opendaylight.openflowplugin.impl;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
 import com.google.common.util.concurrent.ListenableFuture;
 import io.netty.util.HashedWheelTimer;
 import io.netty.util.TimerTask;
+import java.math.BigInteger;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
 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.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.openflowplugin.api.openflow.OFPContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 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.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
+import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
+import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
+import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
 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.protocol.rev130731.FeaturesReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
-
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.TimeUnit;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 
 @RunWith(MockitoJUnitRunner.class)
 public class LifecycleConductorImplTest {
@@ -46,7 +60,7 @@ public class LifecycleConductorImplTest {
     @Mock
     private ServiceChangeListener serviceChangeListener;
     @Mock
-    private ConcurrentHashMap<NodeId, ServiceChangeListener> serviceChangeListeners;
+    private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners;
     @Mock
     private DeviceContext deviceContext;
     @Mock
@@ -67,28 +81,40 @@ public class LifecycleConductorImplTest {
     private ListenableFuture<Void> listenableFuture;
     @Mock
     private StatisticsManager statisticsManager;
+    @Mock
+    private RpcManager rpcManager;
+    @Mock
+    private RpcContext rpcContext;
+    @Mock
+    private DeviceInfo deviceInfo;
 
     private NodeId nodeId = new NodeId("openflow-junit:1");
     private OfpRole ofpRole = OfpRole.NOCHANGE;
-    private long delay = 42;
+    private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
 
     @Before
     public void setUp() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
-        when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+        nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
+        final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
 
-        lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
-        lifecycleConductor.setSafelyDeviceManager(deviceManager);
-        lifecycleConductor.setSafelyStatisticsManager(statisticsManager);
+        lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency, convertorManager);
+        lifecycleConductor.setSafelyManager(deviceManager);
+        lifecycleConductor.setSafelyManager(statisticsManager);
+        lifecycleConductor.setSafelyManager(rpcManager);
 
-        when(connectionContext.getFeatures()).thenReturn(featuresReply);
+        when(deviceManager.gainContext(Mockito.any())).thenReturn(deviceContext);
+        when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+        when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
+        when(rpcManager.gainContext(Mockito.any())).thenReturn(rpcContext);
+        when(deviceInfo.getNodeId()).thenReturn(nodeId);
+        when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
+        when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
+        when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
     }
 
-
-
     @Test
     public void addOneTimeListenerWhenServicesChangesDoneTest() {
-        lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
+        lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
     }
 
@@ -98,9 +124,9 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void notifyServiceChangeListenersTest1() {
-        lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
+        lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
         when(serviceChangeListeners.size()).thenReturn(0);
-        verify(serviceChangeListeners,times(0)).remove(nodeId);
+        verify(serviceChangeListeners,times(0)).remove(deviceInfo);
     }
 
     /**
@@ -108,9 +134,9 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void notifyServiceChangeListenersTest2() {
-        lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
+        lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
-        lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
+        lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
         assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
     }
 
@@ -120,8 +146,8 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void roleInitializationDoneTest1() {
-        lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
-        lifecycleConductor.roleInitializationDone(nodeId,false);
+        lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
+        lifecycleConductor.roleInitializationDone(deviceInfo,false);
         verify(deviceContext,times(1)).shutdownConnection();
     }
 
@@ -130,41 +156,20 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void roleInitializationDoneTest2() {
-        lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
-        lifecycleConductor.roleInitializationDone(nodeId,true);
+        lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
+        lifecycleConductor.roleInitializationDone(deviceInfo,true);
         verify(deviceContext,times(0)).shutdownConnection();
     }
 
     /**
-     * When getDeviceContext returns null nothing should happen
+     * When getDeviceContext returns null raise exception
      */
-    @Test
+    @Test(expected = NullPointerException.class)
     public void roleChangeOnDeviceTest1() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
-        lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,false);
+        when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
+        lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
         verify(deviceContext,times(0)).shutdownConnection();
-        lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
-        verify(deviceContext,times(0)).shutdownConnection();
-    }
-
-    /**
-     * When success flag is set to FALSE connection should be closed
-     */
-    @Test
-    public void roleChangeOnDeviceTest2() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
-        lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
-        verify(deviceContext,times(1)).shutdownConnection();
-    }
-
-    /**
-     * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
-     * device should be skipped
-     */
-    @Test
-    public void roleChangeOnDeviceTest3() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
-        lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,true);
+        lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
         verify(deviceContext,times(0)).shutdownConnection();
     }
 
@@ -173,11 +178,13 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void roleChangeOnDeviceTest4() {
+        final DataBroker dataBroker = mock(DataBroker.class);
+
         when(deviceContext.getDeviceState()).thenReturn(deviceState);
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
-        when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
-        lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMEMASTER,false);
-        verify(statisticsManager).startScheduling(nodeId);
+        when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier));
+        when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
+        lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMEMASTER);
+        verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
     }
 
     /**
@@ -185,30 +192,14 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void roleChangeOnDeviceTest5() {
-        when(deviceContext.getDeviceState()).thenReturn(deviceState);
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
-        when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
-        lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMESLAVE,false);
-        verify(statisticsManager).stopScheduling(nodeId);
-    }
+        final DataBroker dataBroker = mock(DataBroker.class);
 
-    /**
-     * If getDeviceContext returns null nothing should happen
-     */
-    @Test
-    public void gainVersionSafelyTest1() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
-        assertNull(lifecycleConductor.gainVersionSafely(nodeId));
-    }
+        when(deviceContext.getDeviceState()).thenReturn(deviceState);
+        when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier));
+        when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
 
-    /**
-     * If getDeviceContext returns deviceContext getPrimaryConnectionContext() should be called
-     */
-    @Test
-    public void gainVersionSafelyTest2() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
-        lifecycleConductor.gainVersionSafely(nodeId);
-        verify(deviceContext,times(1)).getPrimaryConnectionContext();
+        lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMESLAVE);
+        verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
     }
 
     /**
@@ -216,8 +207,8 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void gainConnectionStateSafelyTest1() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
-        assertNull(lifecycleConductor.gainConnectionStateSafely(nodeId));
+        when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
+        assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
     }
 
     /**
@@ -225,8 +216,7 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void gainConnectionStateSafelyTest2() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
-        lifecycleConductor.gainConnectionStateSafely(nodeId);
+        lifecycleConductor.gainConnectionStateSafely(deviceInfo);
         verify(deviceContext,times(1)).getPrimaryConnectionContext();
     }
 
@@ -235,8 +225,8 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void reserveXidForDeviceMessageTest1() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
-        assertNull(lifecycleConductor.reserveXidForDeviceMessage(nodeId));
+        when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
+        assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
     }
 
     /**
@@ -244,8 +234,7 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void reserveXidForDeviceMessageTest2() {
-        when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
-        lifecycleConductor.reserveXidForDeviceMessage(nodeId);
+        lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
         verify(deviceContext,times(1)).reserveXidForDeviceMessage();
     }
 
@@ -254,7 +243,7 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void deviceStartInitializationDoneTest() {
-        lifecycleConductor.deviceStartInitializationDone(nodeId, false);
+        lifecycleConductor.deviceStartInitializationDone(deviceInfo, false);
         verify(deviceContext,times(1)).shutdownConnection();
     }
 
@@ -263,7 +252,7 @@ public class LifecycleConductorImplTest {
      */
     @Test
     public void deviceInitializationDoneTest() {
-        lifecycleConductor.deviceInitializationDone(nodeId, false);
+        lifecycleConductor.deviceInitializationDone(deviceInfo, false);
         verify(deviceContext,times(1)).shutdownConnection();
     }
 }