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 javax.management.MBeanServer;
+import org.opendaylight.controller.config.persist.api.ConfigPusher;
import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
-import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
-import org.opendaylight.controller.netconf.persist.impl.ConfigPusher;
+import org.opendaylight.controller.netconf.persist.impl.ConfigPusherImpl;
import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
import org.opendaylight.controller.netconf.util.CloseableUtil;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import javax.management.MBeanServer;
-import java.lang.management.ManagementFactory;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-
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";
public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
private List<AutoCloseable> autoCloseables;
+ private volatile BundleContext context;
+ ServiceRegistration<?> registration;
@Override
public void start(final BundleContext context) throws Exception {
- logger.debug("ConfigPersister starting");
+ LOG.debug("ConfigPersister starting");
+ this.context = context;
+
autoCloseables = new ArrayList<>();
PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
long maxWaitForCapabilitiesMillis = getMaxWaitForCapabilitiesMillis(propertiesProvider);
List<ConfigSnapshotHolder> configs = persisterAggregator.loadLastConfigs();
long conflictingVersionTimeoutMillis = getConflictingVersionTimeoutMillis(propertiesProvider);
- logger.trace("Following configs will be pushed: {}", configs);
+ LOG.debug("Following configs will be pushed: {}", configs);
InnerCustomizer innerCustomizer = new InnerCustomizer(configs, maxWaitForCapabilitiesMillis,
conflictingVersionTimeoutMillis, persisterAggregator);
}
@Override
- public synchronized void stop(BundleContext context) throws Exception {
- CloseableUtil.closeAll(autoCloseables);
+ public void stop(BundleContext context) throws Exception {
+ synchronized(autoCloseables) {
+ CloseableUtil.closeAll(autoCloseables);
+ if (registration != null) {
+ registration.unregister();
+ }
+ this.context = null;
+ }
}
@Override
public NetconfOperationProvider addingService(ServiceReference<NetconfOperationProvider> reference) {
- logger.trace("Got OuterCustomizer.addingService {}", reference);
+ LOG.trace("Got OuterCustomizer.addingService {}", reference);
// JMX was registered, track config-netconf-connector
Filter filter;
try {
@Override
public NetconfOperationServiceFactory addingService(ServiceReference<NetconfOperationServiceFactory> reference) {
- logger.trace("Got InnerCustomizer.addingService {}", reference);
+ LOG.trace("Got InnerCustomizer.addingService {}", reference);
NetconfOperationServiceFactory service = reference.getBundle().getBundleContext().getService(reference);
- final ConfigPusher configPusher = new ConfigPusher(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
- logger.debug("Configuration Persister got {}", service);
+ LOG.debug("Creating new job queue");
+
+ final ConfigPusherImpl configPusher = new ConfigPusherImpl(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
+ LOG.debug("Configuration Persister got {}", service);
+ LOG.debug("Context was {}", context);
+ LOG.debug("Registration was {}", registration);
+
final Thread pushingThread = new Thread(new Runnable() {
@Override
public void run() {
try {
- configPusher.pushConfigs(configs);
- } catch (NetconfDocumentedException e) {
- logger.error("Error pushing configs {}",configs);
- throw new IllegalStateException(e);
- }
- logger.info("Configuration Persister initialization completed.");
- ConfigPersisterNotificationHandler jmxNotificationHandler = new ConfigPersisterNotificationHandler(platformMBeanServer, persisterAggregator);
- synchronized (ConfigPersisterActivator.this) {
- autoCloseables.add(jmxNotificationHandler);
+ if(configs != null && !configs.isEmpty()) {
+ configPusher.pushConfigs(configs);
+ }
+ 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) {
+ LOG.info("ConfigPusher thread stopped",e);
}
+ LOG.info("Configuration Persister initialization completed.");
}
}, "config-pusher");
- synchronized (ConfigPersisterActivator.this) {
+ synchronized (autoCloseables) {
autoCloseables.add(new AutoCloseable() {
@Override
- public void close() throws Exception {
+ public void close() {
pushingThread.interrupt();
}
});