Improve cleanup after device disconnected event
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / rpc / RpcManagerImplTest.java
index ce3e4798190341b195110614642370109300e082..62ce9bdade20f254fe879c51bd48df94983d9877 100644 (file)
  */
 package org.opendaylight.openflowplugin.impl.rpc;
 
-import static org.mockito.Mockito.mock;
+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 static org.mockito.Mockito.when;
-import java.math.BigInteger;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-import org.junit.Ignore;
+
+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.openflowjava.protocol.api.connection.ConnectionAdapter;
+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.Xid;
+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.openflowplugin.api.openflow.rpc.RpcContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
-import org.opendaylight.openflowplugin.impl.services.SalFlowServiceImpl;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
+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.yangtools.yang.binding.InstanceIdentifier;
+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;
-import org.opendaylight.yangtools.yang.common.RpcResult;
 
-public class RpcManagerImplTest {
 
-    private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 12;
+@RunWith(MockitoJUnitRunner.class)
+public class RpcManagerImplTest {
 
+    private static final int QUOTA_VALUE = 5;
+    private RpcManagerImpl rpcManager;
 
-    final ProviderContext mockedProviderContext = mock(ProviderContext.class);
-    final RpcManagerImpl rpcManager = new RpcManagerImpl(mockedProviderContext, 500);
-    final DeviceContext mockedDeviceContext = mock(DeviceContext.class);
+    @Mock
+    private ProviderContext rpcProviderRegistry;
+    @Mock
+    private DeviceContext deviceContext;
+    @Mock
+    private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
+    @Mock
+    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() {
+        final NodeKey nodeKey = new NodeKey(nodeId);
+        rpcManager = new RpcManagerImpl(rpcProviderRegistry, QUOTA_VALUE, extensionConverterProvider, convertorExecutor, notificationPublishService);
+        rpcManager.setDeviceInitializationPhaseHandler(deviceINitializationPhaseHandler);
+
+        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(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);
+    }
 
-    @Ignore
     @Test
-    public void deviceConnectedTest() {
+    public void onDeviceContextLevelUpTwice() throws Exception {
+        rpcManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+        expectedException.expect(VerifyException.class);
+        rpcManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+    }
 
-        rpcManager.onDeviceContextLevelUp(mockedDeviceContext);
+    @Test
+    public void testOnDeviceContextLevelUpMaster() throws Exception {
+        rpcManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+        verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo, lifecycleService);
+    }
 
-        verify(mockedProviderContext, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
-                Matchers.any(Class.class), Matchers.any(RpcService.class));
+    @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);
+    }
 
     /**
-     * Tests behavior of RpcContextImpl when calling rpc from MD-SAL
+     * On non null context close and onDeviceContextLevelDown should be called
      */
-    @Ignore
     @Test
-    public void invokeRpcTestExistsCapacityTest() throws InterruptedException, ExecutionException {
-        final ConnectionContext mockedConnectionContext = mock(ConnectionContext.class);
-        final FeaturesReply mockedFeatures = mock(FeaturesReply.class);
-        final BigInteger dummyDatapathId = BigInteger.ONE;
-        final Short dummyVersion = 1;
-        final ConnectionAdapter mockedConnectionAdapter = mock(ConnectionAdapter.class);
-
-        when(mockedFeatures.getDatapathId()).thenReturn(dummyDatapathId);
-        when(mockedFeatures.getVersion()).thenReturn(dummyVersion);
-        when(mockedConnectionContext.getFeatures()).thenReturn(mockedFeatures);
-        when(mockedConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
-        when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedConnectionContext);
-        final Xid mockedXid = mock(Xid.class);
-        final Long dummyXid = 1l;
-        when(mockedXid.getValue()).thenReturn(dummyXid);
-        when(mockedDeviceContext.getReservedXid()).thenReturn(dummyXid);
-
-        invokeRpcTestExistsCapacity(10, true);
-        invokeRpcTestExistsCapacity(0, false);
+    public void onDeviceContextLevelDown1() {
+        rpcManager.addRecordToContexts(deviceInfo, removedContexts);
+        rpcManager.onDeviceContextLevelDown(deviceInfo);
+        verify(removedContexts,times(1)).close();
+        verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceInfo);
     }
 
-    private void invokeRpcTestExistsCapacity(final int capacity, final boolean result) throws InterruptedException,
-            ExecutionException {
-        // TODO: how to invoke service remotely?
-        NodeId nodeId = new NodeId("openflow:1");
-        KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
-        final RpcContextImpl rpcContext = new RpcContextImpl(messageSpy, mockedProviderContext, mockedDeviceContext, capacity);
-        when(mockedProviderContext.getRpcService(SalFlowService.class)).thenReturn(new SalFlowServiceImpl(rpcContext, mockedDeviceContext));
-
-        final SalFlowService salFlowService = mockedProviderContext.getRpcService(SalFlowService.class);
-        final Future<RpcResult<AddFlowOutput>> addedFlow = salFlowService.addFlow(prepareTestingAddFlow());
-    }
 
     /**
-     * @return
+     * On null context only onDeviceContextLevelDown should be called
      */
-    private static AddFlowInput prepareTestingAddFlow() {
-        final AddFlowInputBuilder builder = new AddFlowInputBuilder();
-        builder.setFlowName("dummy flow");
-        builder.setHardTimeout(10000);
+    @Test
+    public void onDeviceContextLevelDown2() {
+        rpcManager.onDeviceContextLevelDown(deviceInfo);
+        verify(removedContexts,never()).close();
+        verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceInfo);
+
+    }
 
-        return builder.build();
+    @Test
+    public void close() {
+        rpcManager.addRecordToContexts(deviceInfo,removedContexts);
+        rpcManager.close();
+        verify(removedContexts,atLeastOnce()).close();
     }
 }