2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.persist.impl.osgi;
11 import com.google.common.annotations.VisibleForTesting;
12 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
13 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
14 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
15 import org.opendaylight.controller.netconf.persist.impl.ConfigPusher;
16 import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
17 import org.osgi.framework.BundleActivator;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.Constants;
20 import org.osgi.framework.Filter;
21 import org.osgi.framework.ServiceReference;
22 import org.osgi.util.tracker.ServiceTracker;
23 import org.osgi.util.tracker.ServiceTrackerCustomizer;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import javax.management.MBeanServer;
28 import java.lang.management.ManagementFactory;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.concurrent.TimeUnit;
33 public class ConfigPersisterActivator implements BundleActivator {
35 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
37 public static final String MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY = "maxWaitForCapabilitiesMillis";
38 private static final long MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT = TimeUnit.MINUTES.toMillis(2);
39 public static final String CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY = "conflictingVersionTimeoutMillis";
40 private static final long CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT = TimeUnit.SECONDS.toMillis(30);
42 public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
44 public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
47 private static final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
49 private List<AutoCloseable> autoCloseables;
53 public void start(final BundleContext context) throws Exception {
54 logger.debug("ConfigPersister starting");
55 autoCloseables = new ArrayList<>();
56 PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
59 final PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
60 autoCloseables.add(persisterAggregator);
61 final long maxWaitForCapabilitiesMillis = getMaxWaitForCapabilitiesMillis(propertiesProvider);
62 final List<ConfigSnapshotHolder> configs = persisterAggregator.loadLastConfigs();
63 final long conflictingVersionTimeoutMillis = getConflictingVersionTimeoutMillis(propertiesProvider);
64 logger.trace("Following configs will be pushed: {}", configs);
65 ServiceTrackerCustomizer<NetconfOperationServiceFactory, NetconfOperationServiceFactory> configNetconfCustomizer = new ServiceTrackerCustomizer<NetconfOperationServiceFactory, NetconfOperationServiceFactory>() {
67 public NetconfOperationServiceFactory addingService(ServiceReference<NetconfOperationServiceFactory> reference) {
68 NetconfOperationServiceFactory service = reference.getBundle().getBundleContext().getService(reference);
69 final ConfigPusher configPusher = new ConfigPusher(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
70 logger.debug("Configuration Persister got %s", service);
71 final Thread pushingThread = new Thread(new Runnable() {
74 configPusher.pushConfigs(configs);
75 logger.info("Configuration Persister initialization completed.");
76 ConfigPersisterNotificationHandler jmxNotificationHandler = new ConfigPersisterNotificationHandler(platformMBeanServer, persisterAggregator);
77 synchronized (ConfigPersisterActivator.this) {
78 autoCloseables.add(jmxNotificationHandler);
82 synchronized (ConfigPersisterActivator.this){
83 autoCloseables.add(new AutoCloseable() {
85 public void close() throws Exception {
86 pushingThread.interrupt();
90 pushingThread.start();
95 public void modifiedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
99 public void removedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
103 Filter filter = context.createFilter(getFilterString());
105 ServiceTracker<NetconfOperationServiceFactory, NetconfOperationServiceFactory> tracker =
106 new ServiceTracker<>(context, filter, configNetconfCustomizer);
112 public static String getFilterString() {
114 "(" + Constants.OBJECTCLASS + "=" + NetconfOperationServiceFactory.class.getName() + ")" +
115 "(name" + "=" + "config-netconf-connector" + ")" +
119 private long getConflictingVersionTimeoutMillis(PropertiesProviderBaseImpl propertiesProvider) {
120 String timeoutProperty = propertiesProvider.getProperty(CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY);
121 return timeoutProperty == null ? CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
124 private long getMaxWaitForCapabilitiesMillis(PropertiesProviderBaseImpl propertiesProvider) {
125 String timeoutProperty = propertiesProvider.getProperty(MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY);
126 return timeoutProperty == null ? MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
130 public synchronized void stop(BundleContext context) throws Exception {
131 Exception lastException = null;
132 for (AutoCloseable autoCloseable : autoCloseables) {
134 autoCloseable.close();
135 } catch (Exception e) {
136 if (lastException == null) {
139 lastException.addSuppressed(e);
143 if (lastException != null) {