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.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
15 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
16 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
17 import org.opendaylight.controller.netconf.persist.impl.ConfigPusher;
18 import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
19 import org.opendaylight.controller.netconf.util.CloseableUtil;
20 import org.osgi.framework.BundleActivator;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.Constants;
23 import org.osgi.framework.Filter;
24 import org.osgi.framework.InvalidSyntaxException;
25 import org.osgi.framework.ServiceReference;
26 import org.osgi.util.tracker.ServiceTracker;
27 import org.osgi.util.tracker.ServiceTrackerCustomizer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import javax.management.MBeanServer;
32 import java.lang.management.ManagementFactory;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.concurrent.TimeUnit;
37 public class ConfigPersisterActivator implements BundleActivator {
39 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
40 private static final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
42 public static final String MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY = "maxWaitForCapabilitiesMillis";
43 private static final long MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT = TimeUnit.MINUTES.toMillis(2);
44 public static final String CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY = "conflictingVersionTimeoutMillis";
45 private static final long CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT = TimeUnit.SECONDS.toMillis(30);
47 public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
49 public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
51 private List<AutoCloseable> autoCloseables;
55 public void start(final BundleContext context) throws Exception {
56 logger.debug("ConfigPersister starting");
57 autoCloseables = new ArrayList<>();
58 PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
60 final PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
61 autoCloseables.add(persisterAggregator);
62 long maxWaitForCapabilitiesMillis = getMaxWaitForCapabilitiesMillis(propertiesProvider);
63 List<ConfigSnapshotHolder> configs = persisterAggregator.loadLastConfigs();
64 long conflictingVersionTimeoutMillis = getConflictingVersionTimeoutMillis(propertiesProvider);
65 logger.debug("Following configs will be pushed: {}", configs);
67 InnerCustomizer innerCustomizer = new InnerCustomizer(configs, maxWaitForCapabilitiesMillis,
68 conflictingVersionTimeoutMillis, persisterAggregator);
69 OuterCustomizer outerCustomizer = new OuterCustomizer(context, innerCustomizer);
70 new ServiceTracker<>(context, NetconfOperationProvider.class, outerCustomizer).open();
73 private long getConflictingVersionTimeoutMillis(PropertiesProviderBaseImpl propertiesProvider) {
74 String timeoutProperty = propertiesProvider.getProperty(CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY);
75 return timeoutProperty == null ? CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
78 private long getMaxWaitForCapabilitiesMillis(PropertiesProviderBaseImpl propertiesProvider) {
79 String timeoutProperty = propertiesProvider.getProperty(MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY);
80 return timeoutProperty == null ? MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
84 public synchronized void stop(BundleContext context) throws Exception {
85 CloseableUtil.closeAll(autoCloseables);
90 public static String getFilterString() {
92 "(" + Constants.OBJECTCLASS + "=" + NetconfOperationServiceFactory.class.getName() + ")" +
93 "(name" + "=" + "config-netconf-connector" + ")" +
97 class OuterCustomizer implements ServiceTrackerCustomizer<NetconfOperationProvider, NetconfOperationProvider> {
98 private final BundleContext context;
99 private final InnerCustomizer innerCustomizer;
101 OuterCustomizer(BundleContext context, InnerCustomizer innerCustomizer) {
102 this.context = context;
103 this.innerCustomizer = innerCustomizer;
107 public NetconfOperationProvider addingService(ServiceReference<NetconfOperationProvider> reference) {
108 logger.trace("Got OuterCustomizer.addingService {}", reference);
109 // JMX was registered, track config-netconf-connector
112 filter = context.createFilter(getFilterString());
113 } catch (InvalidSyntaxException e) {
114 throw new IllegalStateException(e);
116 new ServiceTracker<>(context, filter, innerCustomizer).open();
121 public void modifiedService(ServiceReference<NetconfOperationProvider> reference, NetconfOperationProvider service) {
126 public void removedService(ServiceReference<NetconfOperationProvider> reference, NetconfOperationProvider service) {
131 class InnerCustomizer implements ServiceTrackerCustomizer<NetconfOperationServiceFactory, NetconfOperationServiceFactory> {
132 private final List<ConfigSnapshotHolder> configs;
133 private final PersisterAggregator persisterAggregator;
134 private final long maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis;
137 InnerCustomizer(List<ConfigSnapshotHolder> configs, long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis,
138 PersisterAggregator persisterAggregator) {
139 this.configs = configs;
140 this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
141 this.conflictingVersionTimeoutMillis = conflictingVersionTimeoutMillis;
142 this.persisterAggregator = persisterAggregator;
146 public NetconfOperationServiceFactory addingService(ServiceReference<NetconfOperationServiceFactory> reference) {
147 logger.trace("Got InnerCustomizer.addingService {}", reference);
148 NetconfOperationServiceFactory service = reference.getBundle().getBundleContext().getService(reference);
150 final ConfigPusher configPusher = new ConfigPusher(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
151 logger.debug("Configuration Persister got {}", service);
152 final Thread pushingThread = new Thread(new Runnable() {
156 configPusher.pushConfigs(configs);
157 } catch (NetconfDocumentedException e) {
158 logger.error("Error pushing configs {}",configs);
159 throw new IllegalStateException(e);
161 logger.info("Configuration Persister initialization completed.");
164 * We have completed initial configuration. At this point
165 * it is good idea to perform garbage collection to prune
166 * any garbage we have accumulated during startup.
168 logger.debug("Running post-initialization garbage collection...");
170 logger.debug("Post-initialization garbage collection completed.");
172 ConfigPersisterNotificationHandler jmxNotificationHandler = new ConfigPersisterNotificationHandler(platformMBeanServer, persisterAggregator);
173 synchronized (ConfigPersisterActivator.this) {
174 autoCloseables.add(jmxNotificationHandler);
178 synchronized (ConfigPersisterActivator.this) {
179 autoCloseables.add(new AutoCloseable() {
181 public void close() {
182 pushingThread.interrupt();
186 pushingThread.start();
191 public void modifiedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
195 public void removedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {