Merge "BUG 720 - YANG leaf as JSON input *<*:* couldn't be saved"
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / StatisticsManagerActivator.java
index 521de7e394ee69fd3d5dfdda4bf8f546a1b513c0..c505af49e6d20ec38023aee94c1361feb0a6dc0d 100644 (file)
@@ -5,45 +5,69 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-/*
- * TODO: Handle multipart messages with following flag true 
- * OFPMPF_REPLY_MORE = 1 << 0
- * Better accumulate all the messages and update local cache 
- * and configurational data store
- */
+
 package org.opendaylight.controller.md.statistics.manager;
 
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.statistics.manager.impl.StatisticsManagerImpl;
 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
-import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
 import org.osgi.framework.BundleContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
 
+/**
+ * Statistics Manager Activator
+ *
+ * OSGi bundle activator
+ *
+ */
 public class StatisticsManagerActivator extends AbstractBindingAwareProvider {
 
-    private static ProviderContext pSession;
-    
-    private static StatisticsProvider statsProvider = new StatisticsProvider();
-   
-    @Override
-    public void onSessionInitiated(ProviderContext session) {
-        
-        pSession = session;
-        DataProviderService dps = session.<DataProviderService>getSALService(DataProviderService.class);
-        StatisticsManagerActivator.statsProvider.setDataService(dps);
-        NotificationProviderService nps = session.<NotificationProviderService>getSALService(NotificationProviderService.class);
-        StatisticsManagerActivator.statsProvider.setNotificationService(nps);
-        StatisticsManagerActivator.statsProvider.start();
+    private final static Logger LOG = LoggerFactory.getLogger(StatisticsManagerActivator.class);
+
+    /* TODO move it to ConfigSubsystem */
+    private static final long DEFAULT_MIN_REQUEST_NET_MONITOR_INTERVAL = 3000L;
+    private static final int MAX_NODES_FOR_COLLECTOR = 16;
+
+    private StatisticsManager statsProvider;
 
-    }
-    
     @Override
-    protected void stopImpl(BundleContext context) {
-        StatisticsManagerActivator.statsProvider.close();
+    public void onSessionInitiated(final ProviderContext session) {
+        LOG.info("StatisticsManagerActivator initialization.");
+        try {
+            final DataBroker dataBroker = session.getSALService(DataBroker.class);
+            final NotificationProviderService notifService =
+                    session.getSALService(NotificationProviderService.class);
+            statsProvider = new StatisticsManagerImpl(dataBroker, MAX_NODES_FOR_COLLECTOR);
+            statsProvider.start(notifService, session, DEFAULT_MIN_REQUEST_NET_MONITOR_INTERVAL);
+            LOG.info("StatisticsManagerActivator started successfully.");
+        }
+        catch (final Exception e) {
+            LOG.error("Unexpected error by initialization of StatisticsManagerActivator", e);
+            stopImpl(null);
+        }
     }
-    
-    public static ProviderContext getProviderContext(){
-        return pSession;
+
+    @VisibleForTesting
+    StatisticsManager getStatisticManager() {
+        return statsProvider;
     }
 
+    @Override
+    protected void stopImpl(final BundleContext context) {
+        if (statsProvider != null) {
+            try {
+                statsProvider.close();
+            }
+            catch (final Exception e) {
+                LOG.error("Unexpected error by stopping StatisticsManagerActivator", e);
+            }
+            statsProvider = null;
+        }
+        LOG.info("StatisticsManagerActivator stoped.");
+    }
 }