Improve cleanup after device disconnected event
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / rpc / RpcManagerImplTest.java
index d9815accb83ddd45b0c15857dea52a606ebe9fc4..62ce9bdade20f254fe879c51bd48df94983d9877 100644 (file)
@@ -7,39 +7,54 @@
  */
 package org.opendaylight.openflowplugin.impl.rpc;
 
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
+import com.google.common.base.VerifyException;
+import java.util.concurrent.ConcurrentMap;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 import org.mockito.Matchers;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 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.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 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.LifecycleService;
 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
+import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
+import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
+import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
+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;
 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.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.RpcService;
 
+
 @RunWith(MockitoJUnitRunner.class)
 public class RpcManagerImplTest {
 
-    private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 10;
-
+    private static final int QUOTA_VALUE = 5;
     private RpcManagerImpl rpcManager;
+
     @Mock
     private ProviderContext rpcProviderRegistry;
     @Mock
@@ -47,44 +62,131 @@ public class RpcManagerImplTest {
     @Mock
     private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
     @Mock
-    private ConnectionContext connectionContext;
+    private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
     @Mock
     private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
     @Mock
     private DeviceState deviceState;
     @Mock
+    private MessageSpy mockMsgSpy;
+    @Mock
+    private ConnectionContext connectionContext;
+    @Mock
     private ItemLifeCycleRegistry itemLifeCycleRegistry;
+    @Mock
+    private MessageSpy messageSpy;
+    @Mock
+    private RpcContext removedContexts;
+    @Mock
+    private ConcurrentMap<DeviceInfo, RpcContext> contexts;
+    @Mock
+    private DeviceInfo deviceInfo;
+    @Mock
+    private LifecycleService lifecycleService;
+    @Mock
+    private ExtensionConverterProvider extensionConverterProvider;
+    @Mock
+    private ConvertorExecutor convertorExecutor;
+    @Mock
+    private NotificationPublishService notificationPublishService;
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
 
     private KeyedInstanceIdentifier<Node, NodeKey> nodePath;
 
+    private NodeId nodeId = new NodeId("openflow-junit:1");
+
     @Before
     public void setUp() {
-        nodePath = KeyedInstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow-junit:1")));
-        rpcManager = new RpcManagerImpl(rpcProviderRegistry, 5);
+        final NodeKey nodeKey = new NodeKey(nodeId);
+        rpcManager = new RpcManagerImpl(rpcProviderRegistry, QUOTA_VALUE, extensionConverterProvider, convertorExecutor, notificationPublishService);
         rpcManager.setDeviceInitializationPhaseHandler(deviceINitializationPhaseHandler);
-        FeaturesReply features = new GetFeaturesOutputBuilder()
+
+        GetFeaturesOutput featuresOutput = new GetFeaturesOutputBuilder()
                 .setVersion(OFConstants.OFP_VERSION_1_3)
                 .build();
+
+        FeaturesReply features = featuresOutput;
+
+        Mockito.when(connectionContext.getFeatures()).thenReturn(features);
+        Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+        Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
+        Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
+        Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
+        rpcManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
         Mockito.when(connectionContext.getFeatures()).thenReturn(features);
         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
         Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
-        Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
+        Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
+        Mockito.when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
+        Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeKey.getId());
+        Mockito.when(rpcProviderRegistry.addRoutedRpcImplementation(
+                Matchers.any(), Matchers.any(RpcService.class)))
+                .thenReturn(routedRpcRegistration);
+        Mockito.when(contexts.remove(deviceInfo)).thenReturn(removedContexts);
+        Mockito.when(lifecycleService.getDeviceContext()).thenReturn(deviceContext);
     }
 
     @Test
-    public void testOnDeviceContextLevelUp() {
+    public void onDeviceContextLevelUpTwice() throws Exception {
+        rpcManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+        expectedException.expect(VerifyException.class);
+        rpcManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+    }
 
-        Mockito.when(rpcProviderRegistry.addRoutedRpcImplementation(
-                Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class)))
-                .thenReturn(routedRpcRegistration);
+    @Test
+    public void testOnDeviceContextLevelUpMaster() throws Exception {
+        rpcManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+        verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo, lifecycleService);
+    }
+
+    @Test
+    public void testOnDeviceContextLevelUpSlave() throws Exception {
+        rpcManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+        verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo, lifecycleService);
+    }
+
+    @Test
+    public void testOnDeviceContextLevelUpOther() throws Exception {
+        rpcManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+        verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo, lifecycleService);
+    }
+
+    @Test
+    public void testOnDeviceContextLevelDown() throws Exception {
+        rpcManager.onDeviceContextLevelDown(deviceInfo);
+        verify(deviceTerminationPhaseHandler).onDeviceContextLevelDown(deviceInfo);
+    }
+
+    /**
+     * On non null context close and onDeviceContextLevelDown should be called
+     */
+    @Test
+    public void onDeviceContextLevelDown1() {
+        rpcManager.addRecordToContexts(deviceInfo, removedContexts);
+        rpcManager.onDeviceContextLevelDown(deviceInfo);
+        verify(removedContexts,times(1)).close();
+        verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceInfo);
+    }
+
+
+    /**
+     * On null context only onDeviceContextLevelDown should be called
+     */
+    @Test
+    public void onDeviceContextLevelDown2() {
+        rpcManager.onDeviceContextLevelDown(deviceInfo);
+        verify(removedContexts,never()).close();
+        verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceInfo);
 
-        rpcManager.onDeviceContextLevelUp(deviceContext);
+    }
 
-        Mockito.verify(rpcProviderRegistry, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
-                Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class));
-        Mockito.verify(routedRpcRegistration, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).registerPath(
-                NodeContext.class, nodePath);
-        Mockito.verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceContext);
+    @Test
+    public void close() {
+        rpcManager.addRecordToContexts(deviceInfo,removedContexts);
+        rpcManager.close();
+        verify(removedContexts,atLeastOnce()).close();
     }
 }