Use OSGi DS for MessageIntelligenceAgencyImpl
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / OpenFlowPluginProviderImpl.java
index 7f3fdd663dd062fab7c87e74f0d64b033c681f92..bc59a99103fc24f0de4c540df1c0f42e91412272 100644 (file)
@@ -15,7 +15,6 @@ 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;
@@ -30,13 +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.opendaylight.infrautils.diagstatus.ServiceState;
 import org.opendaylight.infrautils.ready.SystemReadyListener;
 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
@@ -72,8 +64,6 @@ 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;
@@ -98,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,15 +110,12 @@ public class OpenFlowPluginProviderImpl implements
     private ConnectionManager connectionManager;
     private ExecutorService executorService;
     private ContextChainHolderImpl contextChainHolder;
+    private final MessageIntelligenceAgency messageIntelligenceAgency;
     private final DiagStatusProvider diagStatusProvider;
     private final SystemReadyMonitor systemReadyMonitor;
     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 List<SwitchConnectionProvider> switchConnectionProviders,
@@ -142,6 +125,7 @@ public class OpenFlowPluginProviderImpl implements
                                final ClusterSingletonServiceProvider singletonServiceProvider,
                                final EntityOwnershipService entityOwnershipService,
                                final MastershipChangeServiceManager mastershipChangeServiceManager,
+                               final MessageIntelligenceAgency messageIntelligenceAgency,
                                final DiagStatusProvider diagStatusProvider,
                                final SystemReadyMonitor systemReadyMonitor) {
         this.switchConnectionProviders = switchConnectionProviders;
@@ -155,6 +139,7 @@ public class OpenFlowPluginProviderImpl implements
         deviceInitializerProvider = DeviceInitializerProviderFactory.createDefaultProvider();
         config = new OpenFlowProviderConfigImpl(configurationService);
         this.mastershipChangeServiceManager = mastershipChangeServiceManager;
+        this.messageIntelligenceAgency = messageIntelligenceAgency;
         this.diagStatusProvider = diagStatusProvider;
         this.systemReadyMonitor = systemReadyMonitor;
     }
@@ -227,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);
@@ -245,7 +228,7 @@ public class OpenFlowPluginProviderImpl implements
         deviceManager = new DeviceManagerImpl(
                 config,
                 dataBroker,
-                getMessageIntelligenceAgency(),
+                messageIntelligenceAgency,
                 notificationPublishService,
                 hashedWheelTimer,
                 convertorManager,
@@ -325,7 +308,6 @@ public class OpenFlowPluginProviderImpl implements
         gracefulShutdown(roleManager);
         gracefulShutdown(executorService);
         gracefulShutdown(hashedWheelTimer);
-        unregisterMXBean(MESSAGE_INTELLIGENCE_AGENCY_MX_BEAN_NAME);
         diagStatusProvider.reportStatus(ServiceState.UNREGISTERED);
         try {
             if (connectionManager != null) {
@@ -368,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);
-        }
-    }
 }