X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fconfig-persister-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fpersist%2Fimpl%2Fosgi%2FConfigPersisterActivator.java;h=b27bec3c83801a275810e689e0daabbd7b458f47;hp=0a48e6c67dc8cc290c2b81a1b0eb67c05ac7c618;hb=bfd413d87f82ee3ffed67a141a980805950a0f06;hpb=971b179000ef1cc56699de35061cf6f97d4cf36f diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java index 0a48e6c67d..b27bec3c83 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java @@ -8,16 +8,15 @@ package org.opendaylight.controller.netconf.persist.impl.osgi; +import com.google.common.annotations.VisibleForTesting; import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; - +import java.util.concurrent.atomic.AtomicBoolean; import javax.management.MBeanServer; - import org.opendaylight.controller.config.persist.api.ConfigPusher; import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; -import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider; import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory; import org.opendaylight.controller.netconf.persist.impl.ConfigPusherImpl; import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator; @@ -34,17 +33,15 @@ import org.osgi.util.tracker.ServiceTrackerCustomizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.annotations.VisibleForTesting; - public class ConfigPersisterActivator implements BundleActivator { - private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class); + private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterActivator.class); private static final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); public static final String MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY = "maxWaitForCapabilitiesMillis"; private static final long MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT = TimeUnit.MINUTES.toMillis(2); public static final String CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY = "conflictingVersionTimeoutMillis"; - private static final long CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT = TimeUnit.SECONDS.toMillis(30); + private static final long CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT = TimeUnit.MINUTES.toMillis(1); public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister"; @@ -57,7 +54,7 @@ public class ConfigPersisterActivator implements BundleActivator { @Override public void start(final BundleContext context) throws Exception { - logger.debug("ConfigPersister starting"); + LOG.debug("ConfigPersister starting"); this.context = context; autoCloseables = new ArrayList<>(); @@ -68,12 +65,12 @@ public class ConfigPersisterActivator implements BundleActivator { long maxWaitForCapabilitiesMillis = getMaxWaitForCapabilitiesMillis(propertiesProvider); List configs = persisterAggregator.loadLastConfigs(); long conflictingVersionTimeoutMillis = getConflictingVersionTimeoutMillis(propertiesProvider); - logger.debug("Following configs will be pushed: {}", configs); + LOG.debug("Following configs will be pushed: {}", configs); InnerCustomizer innerCustomizer = new InnerCustomizer(configs, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis, persisterAggregator); OuterCustomizer outerCustomizer = new OuterCustomizer(context, innerCustomizer); - new ServiceTracker<>(context, NetconfOperationProvider.class, outerCustomizer).open(); + new ServiceTracker<>(context, NetconfOperationServiceFactory.class, outerCustomizer).open(); } private long getConflictingVersionTimeoutMillis(PropertiesProviderBaseImpl propertiesProvider) { @@ -106,7 +103,7 @@ public class ConfigPersisterActivator implements BundleActivator { ")"; } - class OuterCustomizer implements ServiceTrackerCustomizer { + class OuterCustomizer implements ServiceTrackerCustomizer { private final BundleContext context; private final InnerCustomizer innerCustomizer; @@ -116,8 +113,8 @@ public class ConfigPersisterActivator implements BundleActivator { } @Override - public NetconfOperationProvider addingService(ServiceReference reference) { - logger.trace("Got OuterCustomizer.addingService {}", reference); + public NetconfOperationServiceFactory addingService(ServiceReference reference) { + LOG.trace("Got OuterCustomizer.addingService {}", reference); // JMX was registered, track config-netconf-connector Filter filter; try { @@ -130,12 +127,12 @@ public class ConfigPersisterActivator implements BundleActivator { } @Override - public void modifiedService(ServiceReference reference, NetconfOperationProvider service) { + public void modifiedService(ServiceReference reference, NetconfOperationServiceFactory service) { } @Override - public void removedService(ServiceReference reference, NetconfOperationProvider service) { + public void removedService(ServiceReference reference, NetconfOperationServiceFactory service) { } } @@ -144,7 +141,9 @@ public class ConfigPersisterActivator implements BundleActivator { private final List configs; private final PersisterAggregator persisterAggregator; private final long maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis; - + // This inner customizer has its filter to find the right operation service, but it gets triggered after any + // operation service appears. This means that it could start pushing thread up to N times (N = number of operation services spawned in OSGi) + private final AtomicBoolean alreadyStarted = new AtomicBoolean(false); InnerCustomizer(List configs, long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis, PersisterAggregator persisterAggregator) { @@ -156,15 +155,19 @@ public class ConfigPersisterActivator implements BundleActivator { @Override public NetconfOperationServiceFactory addingService(ServiceReference reference) { - logger.trace("Got InnerCustomizer.addingService {}", reference); + if(alreadyStarted.compareAndSet(false, true) == false) { + //Prevents multiple calls to this method spawning multiple pushing threads + return reference.getBundle().getBundleContext().getService(reference); + } + LOG.trace("Got InnerCustomizer.addingService {}", reference); NetconfOperationServiceFactory service = reference.getBundle().getBundleContext().getService(reference); - logger.debug("Creating new job queue"); + LOG.debug("Creating new job queue"); final ConfigPusherImpl configPusher = new ConfigPusherImpl(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis); - logger.debug("Configuration Persister got {}", service); - logger.debug("Context was {}", context); - logger.debug("Registration was {}", registration); + LOG.debug("Configuration Persister got {}", service); + LOG.debug("Context was {}", context); + LOG.debug("Registration was {}", registration); final Thread pushingThread = new Thread(new Runnable() { @Override @@ -173,12 +176,16 @@ public class ConfigPersisterActivator implements BundleActivator { if(configs != null && !configs.isEmpty()) { configPusher.pushConfigs(configs); } - registration = context.registerService(ConfigPusher.class.getName(), configPusher, null); - configPusher.process(autoCloseables, platformMBeanServer, persisterAggregator); + if(context != null) { + registration = context.registerService(ConfigPusher.class.getName(), configPusher, null); + configPusher.process(autoCloseables, platformMBeanServer, persisterAggregator); + } else { + LOG.warn("Unable to process configs as BundleContext is null"); + } } catch (InterruptedException e) { - logger.info("ConfigPusher thread stopped",e); + LOG.info("ConfigPusher thread stopped",e); } - logger.info("Configuration Persister initialization completed."); + LOG.info("Configuration Persister initialization completed."); } }, "config-pusher"); synchronized (autoCloseables) { @@ -195,10 +202,12 @@ public class ConfigPersisterActivator implements BundleActivator { @Override public void modifiedService(ServiceReference reference, NetconfOperationServiceFactory service) { + LOG.trace("Got InnerCustomizer.modifiedService {}", reference); } @Override public void removedService(ServiceReference reference, NetconfOperationServiceFactory service) { + LOG.trace("Got InnerCustomizer.removedService {}", reference); } }