BUG-4083: Li: Enabling polling of statistics via config subsystem.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / OpenFlowPluginProviderImpl.java
index e272dfb9dc781e17c25af33f90e6f0cd67475094..3b0ae0b1cbddcde8421bae802b9604d9370c47a2 100644 (file)
@@ -13,9 +13,16 @@ import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import java.lang.management.ManagementFactory;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.ObjectName;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
@@ -26,12 +33,15 @@ import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
+import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
 import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
 import org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl;
 import org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl;
 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.TranslatorLibraryUtil;
 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManager;
 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
@@ -45,8 +55,11 @@ import org.slf4j.LoggerFactory;
  */
 public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenFlowPluginExtensionRegistratorProvider {
 
-    protected static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
+    private static final Logger LOG = LoggerFactory.getLogger(OpenFlowPluginProviderImpl.class);
+    private static final MessageIntelligenceAgency messageIntelligenceAgency = new MessageIntelligenceAgencyImpl();
 
+    private final int rpcRequestsQuota;
+    private final long globalNotificationQuota;
     private DeviceManager deviceManager;
     private RpcManager rpcManager;
     private RpcProviderRegistry rpcProviderRegistry;
@@ -60,12 +73,24 @@ public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenF
     private DataBroker dataBroker;
     private OfpRole role;
     private Collection<SwitchConnectionProvider> switchConnectionProviders;
-    private Long rpcRequestsQuota;
+    private boolean switchFeaturesMandatory = false;
+    private boolean isStatisticsPollingOff = false;
 
-    public OpenFlowPluginProviderImpl(final Long rpcRequestsQuota) {
-        this.rpcRequestsQuota = rpcRequestsQuota;
+    public OpenFlowPluginProviderImpl(final long rpcRequestsQuota, final Long globalNotificationQuota) {
+        Preconditions.checkArgument(rpcRequestsQuota > 0 && rpcRequestsQuota <= Integer.MAX_VALUE, "rpcRequestQuota has to be in range <1,%s>", Integer.MAX_VALUE);
+        this.rpcRequestsQuota = (int) rpcRequestsQuota;
+        this.globalNotificationQuota = Preconditions.checkNotNull(globalNotificationQuota);
     }
 
+    @Override
+    public boolean isStatisticsPollingOff() {
+        return isStatisticsPollingOff;
+    }
+
+    @Override
+    public void setIsStatisticsPollingOff(final boolean isStatisticsPollingOff) {
+        this.isStatisticsPollingOff = isStatisticsPollingOff;
+    }
 
     private void startSwitchConnections() {
         final List<ListenableFuture<Boolean>> starterChain = new ArrayList<>(switchConnectionProviders.size());
@@ -90,6 +115,18 @@ public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenF
         });
     }
 
+    public boolean isSwitchFeaturesMandatory() {
+        return switchFeaturesMandatory;
+    }
+
+    public void setSwitchFeaturesMandatory(final boolean switchFeaturesMandatory) {
+        this.switchFeaturesMandatory = switchFeaturesMandatory;
+    }
+
+    public static MessageIntelligenceAgency getMessageIntelligenceAgency() {
+        return OpenFlowPluginProviderImpl.messageIntelligenceAgency;
+    }
+
     @Override
     public void setSwitchConnectionProviders(final Collection<SwitchConnectionProvider> switchConnectionProviders) {
         this.switchConnectionProviders = switchConnectionProviders;
@@ -124,7 +161,10 @@ public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenF
         OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterManager);
 
         connectionManager = new ConnectionManagerImpl();
-        deviceManager = new DeviceManagerImpl(dataBroker);
+
+        registerMXBean(messageIntelligenceAgency);
+
+        deviceManager = new DeviceManagerImpl(dataBroker, messageIntelligenceAgency, switchFeaturesMandatory, globalNotificationQuota);
         statisticsManager = new StatisticsManagerImpl();
         rpcManager = new RpcManagerImpl(rpcProviderRegistry, rpcRequestsQuota);
 
@@ -136,9 +176,27 @@ public class OpenFlowPluginProviderImpl implements OpenFlowPluginProvider, OpenF
         rpcManager.setDeviceInitializationPhaseHandler(deviceManager);
 
         TranslatorLibraryUtil.setBasicTranslatorLibrary(deviceManager);
+        deviceManager.initialize();
+
         startSwitchConnections();
     }
 
+    private static void registerMXBean(final MessageIntelligenceAgency messageIntelligenceAgency) {
+        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+        try {
+            String pathToMxBean = String.format("%s:type=%s",
+                    MessageIntelligenceAgencyMXBean.class.getPackage().getName(),
+                    MessageIntelligenceAgencyMXBean.class.getSimpleName());
+            ObjectName name = new ObjectName(pathToMxBean);
+            mbs.registerMBean(messageIntelligenceAgency, name);
+        } catch (MalformedObjectNameException
+                | NotCompliantMBeanException
+                | MBeanRegistrationException
+                | InstanceAlreadyExistsException e) {
+            LOG.warn("Error registering MBean {}", e);
+        }
+    }
+
     @Override
     public void setNotificationProviderService(final NotificationService notificationProviderService) {
         this.notificationProviderService = notificationProviderService;