Statistics context and manager changes 17/40017/7
authorJozef Bacigal <jbacigal@cisco.com>
Wed, 8 Jun 2016 10:37:55 +0000 (12:37 +0200)
committerJozef Bacigal <jbacigal@cisco.com>
Fri, 17 Jun 2016 10:15:51 +0000 (12:15 +0200)
Change-Id: Ifb9b27f5a6419f959d3faac4a6de4fff60cff3b4
Signed-off-by: Jozef Bacigal <jbacigal@cisco.com>
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/statistics/StatisticsManager.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/LifecycleConductorImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsContextImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsManagerImpl.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/LifecycleConductorImplTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsContextImpMockInitiation.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsContextImplParamTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsContextImplTest.java

index e1588be274db166ca8e3adf085dd9f251afd417e..246114a14946d3d7c48647dc2e85eca5c2e3ce90 100644 (file)
@@ -8,19 +8,20 @@
 
 package org.opendaylight.openflowplugin.api.openflow.statistics;
 
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceLifecycleSupervisor;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 
 /**
- * Created by Martin Bobak &lt;mbobak@cisco.com&gt; on 26.2.2015.
+ * Manager to start or stop scheduling statistics
  */
 public interface StatisticsManager extends DeviceLifecycleSupervisor, DeviceInitializationPhaseHandler,
         DeviceTerminationPhaseHandler, AutoCloseable {
 
-    void startScheduling(NodeId nodeId);
-    void stopScheduling(NodeId nodeId);
+    void startScheduling(DeviceInfo deviceInfo);
+    void stopScheduling(DeviceInfo deviceInfo);
 
     @Override
     void close();
index b55bff8720b3431029b134b7a853f3fe21e8fa26..d3ce2b7590feba389ca3408d3f5715a5ad80ea86 100644 (file)
@@ -125,9 +125,9 @@ public final class LifecycleConductorImpl implements LifecycleConductor, RoleCha
             LOG.info("Role change to {} in role context for node {} was successful, starting/stopping services.", newRole, nodeId);
 
             if (OfpRole.BECOMEMASTER.equals(newRole)) {
-                statisticsManager.startScheduling(nodeId);
+                statisticsManager.startScheduling(deviceContext.getPrimaryConnectionContext().getDeviceInfo());
             } else {
-                statisticsManager.stopScheduling(nodeId);
+                statisticsManager.stopScheduling(deviceContext.getPrimaryConnectionContext().getDeviceInfo());
             }
 
             final ListenableFuture<Void> onClusterRoleChange = deviceContext.onClusterRoleChange(null, newRole);
index eafdc7a4aaf53c144ca115520458f5b870accd3f..6257c93b422117d8660dde2a34d3caa723c90472 100644 (file)
@@ -29,6 +29,7 @@ import javax.annotation.Nullable;
 import javax.annotation.concurrent.GuardedBy;
 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.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
@@ -39,12 +40,11 @@ import org.opendaylight.openflowplugin.impl.rpc.listener.ItemLifecycleListenerIm
 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringOnTheFlyService;
 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class StatisticsContextImpl implements StatisticsContext {
+class StatisticsContextImpl implements StatisticsContext {
 
     private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
     private static final String CONNECTION_CLOSED = "Connection closed.";
@@ -63,12 +63,10 @@ public class StatisticsContextImpl implements StatisticsContext {
     private StatisticsGatheringOnTheFlyService statisticsGatheringOnTheFlyService;
     private Timeout pollTimeout;
 
-    private final LifecycleConductor conductor;
     private volatile boolean schedulingEnabled;
 
-    public StatisticsContextImpl(@CheckForNull final NodeId nodeId, final boolean shuttingDownStatisticsPolling, final LifecycleConductor lifecycleConductor) {
-        this.conductor = lifecycleConductor;
-        this.deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(nodeId));
+    StatisticsContextImpl(@CheckForNull final DeviceInfo deviceInfo, final boolean shuttingDownStatisticsPolling, final LifecycleConductor lifecycleConductor) {
+        this.deviceContext = Preconditions.checkNotNull(lifecycleConductor.getDeviceContext(deviceInfo.getNodeId()));
         this.devState = Preconditions.checkNotNull(deviceContext.getDeviceState());
         this.shuttingDownStatisticsPolling = shuttingDownStatisticsPolling;
         emptyFuture = Futures.immediateFuture(false);
index 898d9941895276fd2d038e62819363e4f4b6e560..4b1a151676a857a4140660e40698c3f1ce4cb9a1 100644 (file)
@@ -92,7 +92,7 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
 
         final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(deviceInfo.getNodeId()));
 
-        final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceInfo.getNodeId(), shuttingDownStatisticsPolling, conductor);
+        final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceInfo, shuttingDownStatisticsPolling, conductor);
         Verify.verify(contexts.putIfAbsent(deviceInfo, statisticsContext) == null, "StatisticsCtx still not closed for Node {}", deviceInfo.getNodeId());
 
         deviceContext.getDeviceState().setDeviceSynchronized(true);
@@ -253,29 +253,29 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
     }
 
     @Override
-    public void startScheduling(final NodeId nodeId) {
+    public void startScheduling(final DeviceInfo deviceInfo) {
         if (shuttingDownStatisticsPolling) {
-            LOG.info("Statistics are shut down for device: {}", nodeId);
+            LOG.info("Statistics are shut down for device: {}", deviceInfo.getNodeId());
             return;
         }
 
-        final StatisticsContext statisticsContext = contexts.get(nodeId);
+        final StatisticsContext statisticsContext = contexts.get(deviceInfo);
 
         if (statisticsContext == null) {
-            LOG.warn("Statistics context not found for device: {}", nodeId);
+            LOG.warn("Statistics context not found for device: {}", deviceInfo.getNodeId());
             return;
         }
 
         if (statisticsContext.isSchedulingEnabled()) {
-            LOG.debug("Statistics scheduling is already enabled for device: {}", nodeId);
+            LOG.debug("Statistics scheduling is already enabled for device: {}", deviceInfo.getNodeId());
             return;
         }
 
-        LOG.info("Scheduling statistics poll for device: {}", nodeId);
-        final DeviceContext deviceContext = conductor.getDeviceContext(nodeId);
+        LOG.info("Scheduling statistics poll for device: {}", deviceInfo.getNodeId());
+        final DeviceContext deviceContext = conductor.getDeviceContext(deviceInfo.getNodeId());
 
         if (deviceContext == null) {
-            LOG.warn("Device context not found for device: {}", nodeId);
+            LOG.warn("Device context not found for device: {}", deviceInfo.getNodeId());
             return;
         }
 
@@ -284,12 +284,12 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
     }
 
     @Override
-    public void stopScheduling(final NodeId nodeId) {
-        LOG.debug("Stopping statistics scheduling for device: {}", nodeId);
-        final StatisticsContext statisticsContext = contexts.get(nodeId);
+    public void stopScheduling(final DeviceInfo deviceInfo) {
+        LOG.debug("Stopping statistics scheduling for device: {}", deviceInfo.getNodeId());
+        final StatisticsContext statisticsContext = contexts.get(deviceInfo);
 
         if (statisticsContext == null) {
-            LOG.warn("Statistics context not found for device: {}", nodeId);
+            LOG.warn("Statistics context not found for device: {}", deviceInfo.getNodeId());
             return;
         }
 
index 1738fe3b130182afb60d6ce5b48383f3dff69df9..66c3a4dbb2bd1502d91dbc997739e6858908bd8e 100644 (file)
@@ -21,9 +21,11 @@ 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.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;
@@ -64,6 +66,8 @@ public class LifecycleConductorImplTest {
     private ListenableFuture<Void> listenableFuture;
     @Mock
     private StatisticsManager statisticsManager;
+    @Mock
+    private DeviceInfo deviceInfo;
 
     private NodeId nodeId = new NodeId("openflow-junit:1");
     private OfpRole ofpRole = OfpRole.NOCHANGE;
@@ -79,6 +83,7 @@ public class LifecycleConductorImplTest {
         lifecycleConductor.setSafelyStatisticsManager(statisticsManager);
 
         when(connectionContext.getFeatures()).thenReturn(featuresReply);
+        when(deviceInfo.getNodeId()).thenReturn(nodeId);
     }
 
 
@@ -174,7 +179,7 @@ public class LifecycleConductorImplTest {
         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);
+        verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
     }
 
     /**
@@ -186,7 +191,7 @@ public class LifecycleConductorImplTest {
         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);
+        verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
     }
 
     /**
index bce92039e282e2d7942734dcb955d89693989bd7..022e2d68e80223257ad93436c5cf600ed2cddbf5 100644 (file)
@@ -19,6 +19,7 @@ import org.mockito.Mockito;
 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
 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.LifecycleConductor;
@@ -49,6 +50,7 @@ class StatisticsContextImpMockInitiation {
     StatisticsGatheringService mockedStatisticsGatheringService;
     StatisticsGatheringOnTheFlyService mockedStatisticsOnFlyGatheringService;
     ConnectionContext mockedConnectionContext;
+    DeviceInfo mockedDeviceInfo;
 
     static final KeyedInstanceIdentifier<Node, NodeKey> dummyNodeII = InstanceIdentifier.create(Nodes.class)
             .child(Node.class, new NodeKey(new NodeId("dummyNodeId")));
@@ -62,6 +64,7 @@ class StatisticsContextImpMockInitiation {
         mockedStatisticsOnFlyGatheringService = mock(StatisticsGatheringOnTheFlyService.class);
         mockedConnectionContext = mock(ConnectionContext.class);
         mockedDeviceState = mock(DeviceState.class);
+        mockedDeviceInfo = mock(DeviceInfo.class);
 
         final FeaturesReply mockedFeatures = mock(FeaturesReply.class);
         final MessageSpy mockedMessageSpy = mock(MessageSpy.class);
index 31e74a8b3fcda53964d772d6cd393f1b0b3d7085..4a249885db639b3d59679519f40334c1d34bda71 100644 (file)
@@ -64,7 +64,7 @@ public class StatisticsContextImplParamTest extends StatisticsContextImpMockInit
     public void gatherDynamicDataTest() {
 
 
-        final StatisticsContextImpl statisticsContext = new StatisticsContextImpl(mockedDeviceContext.getDeviceState().getNodeId(), false, mockConductor);
+        final StatisticsContextImpl statisticsContext = new StatisticsContextImpl(mockedDeviceInfo, false, mockConductor);
 
         final ListenableFuture<RpcResult<List<MultipartReply>>> rpcResult = immediateFuture(RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build());
         when(mockedStatisticsGatheringService.getStatisticsOfType(any(EventIdentifier.class), any(MultipartType
index 05cd073993a75c3e41d917ab122b48574183a81c..2006ca210f5920b19a1e61192a2dc7b33d5da920 100644 (file)
@@ -57,7 +57,7 @@ public class StatisticsContextImplTest extends StatisticsContextImpMockInitiatio
     }
 
     private void initStatisticsContext() {
-        statisticsContext = new StatisticsContextImpl(mockedDeviceContext.getDeviceState().getNodeId(), false, mockConductor);
+        statisticsContext = new StatisticsContextImpl(mockedDeviceInfo, false, mockConductor);
         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
     }
@@ -75,7 +75,7 @@ public class StatisticsContextImplTest extends StatisticsContextImpMockInitiatio
      */
     @Test
     public void testClose() throws Exception {
-        final StatisticsContextImpl statisticsContext = new StatisticsContextImpl(mockedDeviceContext.getDeviceState().getNodeId(), false, mockConductor);
+        final StatisticsContextImpl statisticsContext = new StatisticsContextImpl(mockedDeviceInfo, false, mockConductor);
         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
         statisticsContext.close();
         try {