Use OSGi DS for MessageIntelligenceAgencyImpl
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / OpenFlowPluginProviderImpl.java
index f1970937805f11559105c445e533e90177cb0a8b..bc59a99103fc24f0de4c540df1c0f42e91412272 100644 (file)
@@ -11,14 +11,13 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
 import com.google.common.util.concurrent.SettableFuture;
 import io.netty.util.HashedWheelTimer;
 import io.netty.util.Timer;
-import java.lang.management.ManagementFactory;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
@@ -30,15 +29,6 @@ import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import javax.management.InstanceAlreadyExistsException;
-import javax.management.InstanceNotFoundException;
-import javax.management.MBeanRegistrationException;
-import javax.management.MBeanServer;
-import javax.management.MalformedObjectNameException;
-import javax.management.NotCompliantMBeanException;
-import javax.management.ObjectName;
-import org.apache.aries.blueprint.annotation.service.Reference;
-import org.apache.aries.blueprint.annotation.service.Service;
 import org.opendaylight.infrautils.diagstatus.ServiceState;
 import org.opendaylight.infrautils.ready.SystemReadyListener;
 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
@@ -46,11 +36,10 @@ import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 import org.opendaylight.mdsal.binding.api.RpcProviderService;
 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.opendaylight.openflowjava.protocol.api.connection.OpenflowDiagStatusProvider;
+import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
-import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProviderList;
-import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager;
+import org.opendaylight.openflowplugin.api.openflow.FlowGroupInfoHistories;
+import org.opendaylight.openflowplugin.api.openflow.FlowGroupInfoHistory;
 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProvider;
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
@@ -75,23 +64,22 @@ import org.opendaylight.openflowplugin.impl.protocol.serialization.SerializerInj
 import org.opendaylight.openflowplugin.impl.role.RoleManagerImpl;
 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
 import org.opendaylight.openflowplugin.impl.statistics.StatisticsManagerImpl;
-import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
-import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyMXBean;
 import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
 import org.opendaylight.openflowplugin.impl.util.TranslatorLibraryUtil;
 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-@Service(classes = { OpenFlowPluginProvider.class, OpenFlowPluginExtensionRegistratorProvider.class })
 public class OpenFlowPluginProviderImpl implements
         OpenFlowPluginProvider,
         OpenFlowPluginExtensionRegistratorProvider,
+        FlowGroupInfoHistories,
         SystemReadyListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
@@ -100,12 +88,8 @@ public class OpenFlowPluginProviderImpl implements
     private static final long TICK_DURATION = 10;
     private static final String POOL_NAME = "ofppool";
 
-    private static final MessageIntelligenceAgency MESSAGE_INTELLIGENCE_AGENCY = new MessageIntelligenceAgencyImpl();
-    private static final String MESSAGE_INTELLIGENCE_AGENCY_MX_BEAN_NAME = String
-            .format("%s:type=%s",
-                    MessageIntelligenceAgencyMXBean.class.getPackage().getName(),
-                    MessageIntelligenceAgencyMXBean.class.getSimpleName());
-
+    // TODO: Split this out into a separate component, which requires proper timer cancellation from all users. But is
+    //       that worth the complications?
     private final HashedWheelTimer hashedWheelTimer =
             new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, TICKS_PER_WHEEL);
     private final NotificationPublishService notificationPublishService;
@@ -124,44 +108,40 @@ public class OpenFlowPluginProviderImpl implements
     private StatisticsManager statisticsManager;
     private RoleManager roleManager;
     private ConnectionManager connectionManager;
-    private ListeningExecutorService executorService;
+    private ExecutorService executorService;
     private ContextChainHolderImpl contextChainHolder;
-    private final OpenflowDiagStatusProvider openflowDiagStatusProvider;
+    private final MessageIntelligenceAgency messageIntelligenceAgency;
+    private final DiagStatusProvider diagStatusProvider;
     private final SystemReadyMonitor systemReadyMonitor;
-    private final FlowGroupCacheManager flowGroupCacheManager;
     private final SettableFuture<Void> fullyStarted = SettableFuture.create();
     private static final String OPENFLOW_SERVICE_NAME = "OPENFLOW";
 
-    public static MessageIntelligenceAgency getMessageIntelligenceAgency() {
-        return MESSAGE_INTELLIGENCE_AGENCY;
-    }
-
     @Inject
     public OpenFlowPluginProviderImpl(final ConfigurationService configurationService,
-                               final SwitchConnectionProviderList switchConnectionProviders,
-                               final PingPongDataBroker pingPongDataBroker,
-                               final @Reference RpcProviderService rpcProviderRegistry,
-                               final @Reference NotificationPublishService notificationPublishService,
-                               final @Reference ClusterSingletonServiceProvider singletonServiceProvider,
-                               final @Reference EntityOwnershipService entityOwnershipService,
+                               final List<SwitchConnectionProvider> switchConnectionProviders,
+                               final DataBroker dataBroker,
+                               final RpcProviderService rpcProviderRegistry,
+                               final NotificationPublishService notificationPublishService,
+                               final ClusterSingletonServiceProvider singletonServiceProvider,
+                               final EntityOwnershipService entityOwnershipService,
                                final MastershipChangeServiceManager mastershipChangeServiceManager,
-                               final @Reference OpenflowDiagStatusProvider openflowDiagStatusProvider,
-                               final @Reference SystemReadyMonitor systemReadyMonitor,
-                               final FlowGroupCacheManager flowGroupCacheManager) {
+                               final MessageIntelligenceAgency messageIntelligenceAgency,
+                               final DiagStatusProvider diagStatusProvider,
+                               final SystemReadyMonitor systemReadyMonitor) {
         this.switchConnectionProviders = switchConnectionProviders;
-        this.dataBroker = pingPongDataBroker;
+        this.dataBroker = new PingPongDataBroker(dataBroker);
         this.rpcProviderRegistry = rpcProviderRegistry;
         this.notificationPublishService = notificationPublishService;
-        this.singletonServicesProvider = singletonServiceProvider;
+        singletonServicesProvider = singletonServiceProvider;
         this.entityOwnershipService = entityOwnershipService;
         convertorManager = ConvertorManagerFactory.createDefaultManager();
         extensionConverterManager = new ExtensionConverterManagerImpl();
         deviceInitializerProvider = DeviceInitializerProviderFactory.createDefaultProvider();
         config = new OpenFlowProviderConfigImpl(configurationService);
         this.mastershipChangeServiceManager = mastershipChangeServiceManager;
-        this.openflowDiagStatusProvider = openflowDiagStatusProvider;
+        this.messageIntelligenceAgency = messageIntelligenceAgency;
+        this.diagStatusProvider = diagStatusProvider;
         this.systemReadyMonitor = systemReadyMonitor;
-        this.flowGroupCacheManager = flowGroupCacheManager;
     }
 
     @Override
@@ -169,7 +149,7 @@ public class OpenFlowPluginProviderImpl implements
         LOG.info("onSystemBootReady() received, starting the switch connections");
         Futures.addCallback(Futures.allAsList(switchConnectionProviders.stream().map(switchConnectionProvider -> {
             // Inject OpenFlowPlugin custom serializers and deserializers into OpenFlowJava
-            if (config.isUseSingleLayerSerialization()) {
+            if (config.getUseSingleLayerSerialization()) {
                 SerializerInjector.injectSerializers(switchConnectionProvider,
                         switchConnectionProvider.getConfiguration().isGroupAddModEnabled());
                 DeserializerInjector.injectDeserializers(switchConnectionProvider);
@@ -184,14 +164,14 @@ public class OpenFlowPluginProviderImpl implements
             @Override
             public void onSuccess(final List<Boolean> result) {
                 LOG.info("All switchConnectionProviders are up and running ({}).", result.size());
-                openflowDiagStatusProvider.reportStatus(OPENFLOW_SERVICE_NAME, ServiceState.OPERATIONAL);
+                diagStatusProvider.reportStatus(ServiceState.OPERATIONAL);
                 fullyStarted.set(null);
             }
 
             @Override
             public void onFailure(final Throwable throwable) {
                 LOG.warn("Some switchConnectionProviders failed to start.", throwable);
-                openflowDiagStatusProvider.reportStatus(OPENFLOW_SERVICE_NAME, throwable);
+                diagStatusProvider.reportStatus(ServiceState.ERROR, throwable);
                 fullyStarted.setException(throwable);
             }
         }, MoreExecutors.directExecutor());
@@ -206,7 +186,7 @@ public class OpenFlowPluginProviderImpl implements
         final ListenableFuture<List<Boolean>> listListenableFuture =
                 Futures.allAsList(switchConnectionProviders.stream().map(switchConnectionProvider -> {
                     // Revert deserializers to their original state
-                    if (config.isUseSingleLayerSerialization()) {
+                    if (config.getUseSingleLayerSerialization()) {
                         DeserializerInjector.revertDeserializers(switchConnectionProvider);
                     }
 
@@ -232,8 +212,6 @@ public class OpenFlowPluginProviderImpl implements
     @Override
     @PostConstruct
     public void initialize() {
-        registerMXBean(MESSAGE_INTELLIGENCE_AGENCY, MESSAGE_INTELLIGENCE_AGENCY_MX_BEAN_NAME);
-
         // TODO: copied from OpenFlowPluginProvider (Helium) misusesing the old way of distributing extension converters
         // TODO: rewrite later!
         OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterManager);
@@ -241,16 +219,16 @@ public class OpenFlowPluginProviderImpl implements
         // Creates a thread pool that creates new threads as needed, but will reuse previously
         // constructed threads when they are available.
         // Threads that have not been used for x seconds are terminated and removed from the cache.
-        executorService = MoreExecutors.listeningDecorator(new ThreadPoolLoggingExecutor(
+        executorService = new ThreadPoolLoggingExecutor(
                 config.getThreadPoolMinThreads().toJava(),
                 config.getThreadPoolMaxThreads().getValue().toJava(),
                 config.getThreadPoolTimeout().toJava(),
-                TimeUnit.SECONDS, new SynchronousQueue<>(), POOL_NAME));
+                TimeUnit.SECONDS, new SynchronousQueue<>(), POOL_NAME);
 
         deviceManager = new DeviceManagerImpl(
                 config,
                 dataBroker,
-                getMessageIntelligenceAgency(),
+                messageIntelligenceAgency,
                 notificationPublishService,
                 hashedWheelTimer,
                 convertorManager,
@@ -265,8 +243,7 @@ public class OpenFlowPluginProviderImpl implements
                 rpcProviderRegistry,
                 extensionConverterManager,
                 convertorManager,
-                notificationPublishService,
-                flowGroupCacheManager);
+                notificationPublishService);
 
         statisticsManager = new StatisticsManagerImpl(
                 config,
@@ -303,6 +280,16 @@ public class OpenFlowPluginProviderImpl implements
         return extensionConverterManager;
     }
 
+    @Override
+    public Map<NodeId, FlowGroupInfoHistory> getAllFlowGroupHistories() {
+        return deviceManager.getAllFlowGroupHistories();
+    }
+
+    @Override
+    public FlowGroupInfoHistory getFlowGroupHistory(final NodeId nodeId) {
+        return deviceManager.getFlowGroupHistory(nodeId);
+    }
+
     @Override
     @PreDestroy
     @SuppressWarnings("checkstyle:IllegalCatch")
@@ -321,8 +308,7 @@ public class OpenFlowPluginProviderImpl implements
         gracefulShutdown(roleManager);
         gracefulShutdown(executorService);
         gracefulShutdown(hashedWheelTimer);
-        unregisterMXBean(MESSAGE_INTELLIGENCE_AGENCY_MX_BEAN_NAME);
-        openflowDiagStatusProvider.reportStatus(ServiceState.UNREGISTERED);
+        diagStatusProvider.reportStatus(ServiceState.UNREGISTERED);
         try {
             if (connectionManager != null) {
                 connectionManager.close();
@@ -364,28 +350,5 @@ public class OpenFlowPluginProviderImpl implements
         }
     }
 
-    private static void registerMXBean(final Object bean, final String beanName) {
-        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
-
-        try {
-            mbs.registerMBean(bean, new ObjectName(beanName));
-        } catch (MalformedObjectNameException
-                | NotCompliantMBeanException
-                | MBeanRegistrationException
-                | InstanceAlreadyExistsException e) {
-            LOG.warn("Error registering MBean {}", beanName, e);
-        }
-    }
 
-    private static void unregisterMXBean(final String beanName) {
-        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
-
-        try {
-            mbs.unregisterMBean(new ObjectName(beanName));
-        } catch (InstanceNotFoundException
-                | MBeanRegistrationException
-                | MalformedObjectNameException e) {
-            LOG.warn("Error unregistering MBean {}", beanName, e);
-        }
-    }
 }