Bug 1484 - StatisticManager performance improvement refactoring
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / StatisticsManagerActivator.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.md.statistics.manager;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.statistics.manager.impl.StatisticsManagerImpl;
13 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
16 import org.osgi.framework.BundleContext;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.common.annotations.VisibleForTesting;
21
22 /**
23  * Statistics Manager Activator
24  *
25  * OSGi bundle activator
26  *
27  */
28 public class StatisticsManagerActivator extends AbstractBindingAwareProvider {
29
30     private final static Logger LOG = LoggerFactory.getLogger(StatisticsManagerActivator.class);
31
32     /* TODO move it to ConfigSubsystem */
33     private static final long DEFAULT_MIN_REQUEST_NET_MONITOR_INTERVAL = 3000L;
34     private static final int MAX_NODES_FOR_COLLECTOR = 8;
35
36     private StatisticsManager statsProvider;
37
38     @Override
39     public void onSessionInitiated(final ProviderContext session) {
40         LOG.info("StatisticsManagerActivator initialization.");
41         try {
42             final DataBroker dataBroker = session.getSALService(DataBroker.class);
43             final NotificationProviderService notifService =
44                     session.getSALService(NotificationProviderService.class);
45             statsProvider = new StatisticsManagerImpl(dataBroker, MAX_NODES_FOR_COLLECTOR);
46             statsProvider.start(notifService, session, DEFAULT_MIN_REQUEST_NET_MONITOR_INTERVAL);
47             LOG.info("StatisticsManagerActivator started successfully.");
48         }
49         catch (final Exception e) {
50             LOG.error("Unexpected error by initialization of StatisticsManagerActivator", e);
51             stopImpl(null);
52         }
53     }
54
55     @VisibleForTesting
56     StatisticsManager getStatisticManager() {
57         return statsProvider;
58     }
59
60     @Override
61     protected void stopImpl(final BundleContext context) {
62         if (statsProvider != null) {
63             try {
64                 statsProvider.close();
65             }
66             catch (final Exception e) {
67                 LOG.error("Unexpected error by stopping StatisticsManagerActivator", e);
68             }
69             statsProvider = null;
70         }
71         LOG.info("StatisticsManagerActivator stoped.");
72     }
73 }