Switch to MD-SAL APIs
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsManagerImplTest.java
index 91058b031459838846daeffb8620878d174a613c..ac53e5a2c82de1b75d38075e523af3fc2221ae3b 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -8,11 +8,13 @@
 package org.opendaylight.openflowplugin.impl.statistics;
 
 import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.lang.reflect.Field;
-import java.math.BigInteger;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
@@ -21,16 +23,14 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.Matchers;
+import org.mockito.ArgumentMatchers;
 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.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.RpcProviderService;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
-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;
@@ -40,7 +40,6 @@ import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgColl
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ReconciliationFrameworkRegistrar;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
-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;
@@ -52,15 +51,16 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsWorkMode;
+import org.opendaylight.yangtools.concepts.ObjectRegistration;
 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 @RunWith(MockitoJUnitRunner.class)
 public class StatisticsManagerImplTest {
 
@@ -83,13 +83,13 @@ public class StatisticsManagerImplTest {
     @Mock
     private DeviceInfo mockedDeviceInfo;
     @Mock
-    private RpcProviderRegistry rpcProviderRegistry;
+    private RpcProviderService rpcProviderRegistry;
     @Mock
     private OutboundQueue outboundQueue;
     @Mock
     private MultiMsgCollector multiMagCollector;
     @Mock
-    private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> serviceControlRegistration;
+    private ObjectRegistration<StatisticsManagerControlService> serviceControlRegistration;
     @Mock
     private DeviceInfo deviceInfo;
     @Mock
@@ -107,38 +107,9 @@ public class StatisticsManagerImplTest {
                 .create(Nodes.class)
                 .child(Node.class, new NodeKey(new NodeId("openflow:10")));
 
-        when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
-        when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
-        when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
-        when(mockedPrimConnectionContext.getNodeId()).thenReturn(NODE_ID);
-        when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
-
-        when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
-        when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
-        when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
-        when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
-        when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
-        when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
-        when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
-        when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
-        when(mockedDeviceInfo.getNodeId()).thenReturn(NODE_ID);
-
-        when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
-        when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
-        when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
-        when(mockedDeviceContext.getDeviceFlowRegistry())
-            .thenReturn(new DeviceFlowRegistryImpl(OFConstants.OFP_VERSION_1_3, dataBroker, nodePath));
-        when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
-        when(mockedDeviceContext.getMultiMsgCollector(
-                Matchers.<RequestContext<List<MultipartReply>>>any())).thenAnswer(
-                invocation -> {
-                    currentRequestContext = (RequestContext<List<MultipartReply>>) invocation.getArguments()[0];
-                    return multiMagCollector;
-                }
-        );
-        when(rpcProviderRegistry.addRpcImplementation(
-                Matchers.eq(StatisticsManagerControlService.class),
-                Matchers.<StatisticsManagerControlService>any())).thenReturn(serviceControlRegistration);
+        when(rpcProviderRegistry.registerRpcImplementation(
+                eq(StatisticsManagerControlService.class),
+                ArgumentMatchers.any())).thenReturn(serviceControlRegistration);
 
         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
         final long basicTimerDelay = 3000L;
@@ -150,7 +121,8 @@ public class StatisticsManagerImplTest {
                         .setMaximumTimerDelay(new NonZeroUint32Type(maximumTimerDelay))
                         .setIsStatisticsPollingOn(false)
                         .build(), rpcProviderRegistry,
-                convertorManager);
+                convertorManager,
+                MoreExecutors.newDirectExecutorService());
     }
 
     private static Map<DeviceInfo, StatisticsContext> getContextsMap(final StatisticsManagerImpl statisticsManager)
@@ -164,7 +136,7 @@ public class StatisticsManagerImplTest {
 
     @Test
     public void testGetStatisticsWorkMode() throws Exception {
-        final Future<RpcResult<GetStatisticsWorkModeOutput>> workMode = statisticsManager.getStatisticsWorkMode();
+        final Future<RpcResult<GetStatisticsWorkModeOutput>> workMode = statisticsManager.getStatisticsWorkMode(null);
         Assert.assertTrue(workMode.isDone());
         Assert.assertTrue(workMode.get().isSuccessful());
         assertNotNull(workMode.get().getResult());
@@ -174,7 +146,6 @@ public class StatisticsManagerImplTest {
     /**
      * switching to {@link StatisticsWorkMode#FULLYDISABLED}; no pollTimeout and no lifecycleRegistry.
      *
-     * @throws Exception
      */
     @Test
     public void testChangeStatisticsWorkMode1() throws Exception {
@@ -186,14 +157,14 @@ public class StatisticsManagerImplTest {
                 new ChangeStatisticsWorkModeInputBuilder()
                         .setMode(StatisticsWorkMode.FULLYDISABLED);
 
-        final Future<RpcResult<Void>> workMode = statisticsManager
+        final ListenableFuture<RpcResult<ChangeStatisticsWorkModeOutput>> workMode = statisticsManager
                 .changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
 
         checkWorkModeChangeOutcome(workMode);
         verify(statisticContext).disableGathering();
     }
 
-    private static void checkWorkModeChangeOutcome(Future<RpcResult<Void>> workMode)
+    private static void checkWorkModeChangeOutcome(ListenableFuture<RpcResult<ChangeStatisticsWorkModeOutput>> workMode)
             throws InterruptedException, ExecutionException {
         Assert.assertTrue(workMode.isDone());
         Assert.assertTrue(workMode.get().isSuccessful());
@@ -203,7 +174,6 @@ public class StatisticsManagerImplTest {
     /**
      * Switching to {@link StatisticsWorkMode#FULLYDISABLED}; with pollTimeout and lifecycleRegistry.
      *
-     * @throws Exception
      */
     @Test
     public void testChangeStatisticsWorkMode2() throws Exception {
@@ -215,7 +185,7 @@ public class StatisticsManagerImplTest {
                 new ChangeStatisticsWorkModeInputBuilder()
                         .setMode(StatisticsWorkMode.FULLYDISABLED);
 
-        Future<RpcResult<Void>> workMode = statisticsManager
+        ListenableFuture<RpcResult<ChangeStatisticsWorkModeOutput>> workMode = statisticsManager
             .changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
         checkWorkModeChangeOutcome(workMode);
 
@@ -223,10 +193,9 @@ public class StatisticsManagerImplTest {
     }
 
     /**
-     * switching to {@link StatisticsWorkMode#FULLYDISABLED} and back
-     * to {@link StatisticsWorkMode#COLLECTALL}; with lifecycleRegistry and pollTimeout
+     * Tests switching to {@link StatisticsWorkMode#FULLYDISABLED} and back
+     * to {@link StatisticsWorkMode#COLLECTALL}; with lifecycleRegistry and pollTimeout.
      *
-     * @throws Exception
      */
     @Test
     public void testChangeStatisticsWorkMode3() throws Exception {
@@ -238,7 +207,7 @@ public class StatisticsManagerImplTest {
                 new ChangeStatisticsWorkModeInputBuilder()
                         .setMode(StatisticsWorkMode.FULLYDISABLED);
 
-        Future<RpcResult<Void>> workMode;
+        ListenableFuture<RpcResult<ChangeStatisticsWorkModeOutput>> workMode;
         workMode = statisticsManager.changeStatisticsWorkMode(
                 changeStatisticsWorkModeInputBld.build());
         checkWorkModeChangeOutcome(workMode);