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 java.lang.management.ManagementFactory;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.atomic.AtomicBoolean;
17 import javax.management.MBeanServer;
18 import org.opendaylight.controller.config.persist.api.ConfigPusher;
19 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
20 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
21 import org.opendaylight.controller.netconf.persist.impl.ConfigPusherImpl;
22 import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
23 import org.opendaylight.controller.netconf.util.CloseableUtil;
24 import org.osgi.framework.BundleActivator;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.Constants;
27 import org.osgi.framework.Filter;
28 import org.osgi.framework.InvalidSyntaxException;
29 import org.osgi.framework.ServiceReference;
30 import org.osgi.framework.ServiceRegistration;
31 import org.osgi.util.tracker.ServiceTracker;
32 import org.osgi.util.tracker.ServiceTrackerCustomizer;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class ConfigPersisterActivator implements BundleActivator {
38 private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterActivator.class);
39 private static final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
41 public static final String MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY = "maxWaitForCapabilitiesMillis";
42 private static final long MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT = TimeUnit.MINUTES.toMillis(2);
43 public static final String CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY = "conflictingVersionTimeoutMillis";
44 private static final long CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT = TimeUnit.MINUTES.toMillis(1);
46 public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
48 public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
50 private List<AutoCloseable> autoCloseables;
51 private volatile BundleContext context;
53 ServiceRegistration<?> registration;
56 public void start(final BundleContext context) throws Exception {
57 LOG.debug("ConfigPersister starting");
58 this.context = context;
60 autoCloseables = new ArrayList<>();
61 PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
63 final PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
64 autoCloseables.add(persisterAggregator);
65 long maxWaitForCapabilitiesMillis = getMaxWaitForCapabilitiesMillis(propertiesProvider);
66 List<ConfigSnapshotHolder> configs = persisterAggregator.loadLastConfigs();
67 long conflictingVersionTimeoutMillis = getConflictingVersionTimeoutMillis(propertiesProvider);
68 LOG.debug("Following configs will be pushed: {}", configs);
70 InnerCustomizer innerCustomizer = new InnerCustomizer(configs, maxWaitForCapabilitiesMillis,
71 conflictingVersionTimeoutMillis, persisterAggregator);
72 OuterCustomizer outerCustomizer = new OuterCustomizer(context, innerCustomizer);
73 new ServiceTracker<>(context, NetconfOperationServiceFactory.class, outerCustomizer).open();
76 private long getConflictingVersionTimeoutMillis(PropertiesProviderBaseImpl propertiesProvider) {
77 String timeoutProperty = propertiesProvider.getProperty(CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY);
78 return timeoutProperty == null ? CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
81 private long getMaxWaitForCapabilitiesMillis(PropertiesProviderBaseImpl propertiesProvider) {
82 String timeoutProperty = propertiesProvider.getProperty(MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY);
83 return timeoutProperty == null ? MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
87 public void stop(BundleContext context) throws Exception {
88 synchronized(autoCloseables) {
89 CloseableUtil.closeAll(autoCloseables);
90 if (registration != null) {
91 registration.unregister();
99 public static String getFilterString() {
101 "(" + Constants.OBJECTCLASS + "=" + NetconfOperationServiceFactory.class.getName() + ")" +
102 "(name" + "=" + "config-netconf-connector" + ")" +
106 class OuterCustomizer implements ServiceTrackerCustomizer<NetconfOperationServiceFactory, NetconfOperationServiceFactory> {
107 private final BundleContext context;
108 private final InnerCustomizer innerCustomizer;
110 OuterCustomizer(BundleContext context, InnerCustomizer innerCustomizer) {
111 this.context = context;
112 this.innerCustomizer = innerCustomizer;
116 public NetconfOperationServiceFactory addingService(ServiceReference<NetconfOperationServiceFactory> reference) {
117 LOG.trace("Got OuterCustomizer.addingService {}", reference);
118 // JMX was registered, track config-netconf-connector
121 filter = context.createFilter(getFilterString());
122 } catch (InvalidSyntaxException e) {
123 throw new IllegalStateException(e);
125 new ServiceTracker<>(context, filter, innerCustomizer).open();
130 public void modifiedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
135 public void removedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
140 class InnerCustomizer implements ServiceTrackerCustomizer<NetconfOperationServiceFactory, NetconfOperationServiceFactory> {
141 private final List<ConfigSnapshotHolder> configs;
142 private final PersisterAggregator persisterAggregator;
143 private final long maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis;
144 // This inner customizer has its filter to find the right operation service, but it gets triggered after any
145 // operation service appears. This means that it could start pushing thread up to N times (N = number of operation services spawned in OSGi)
146 private final AtomicBoolean alreadyStarted = new AtomicBoolean(false);
148 InnerCustomizer(List<ConfigSnapshotHolder> configs, long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis,
149 PersisterAggregator persisterAggregator) {
150 this.configs = configs;
151 this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
152 this.conflictingVersionTimeoutMillis = conflictingVersionTimeoutMillis;
153 this.persisterAggregator = persisterAggregator;
157 public NetconfOperationServiceFactory addingService(ServiceReference<NetconfOperationServiceFactory> reference) {
158 if(alreadyStarted.compareAndSet(false, true) == false) {
159 //Prevents multiple calls to this method spawning multiple pushing threads
160 return reference.getBundle().getBundleContext().getService(reference);
162 LOG.trace("Got InnerCustomizer.addingService {}", reference);
163 NetconfOperationServiceFactory service = reference.getBundle().getBundleContext().getService(reference);
165 LOG.debug("Creating new job queue");
167 final ConfigPusherImpl configPusher = new ConfigPusherImpl(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
168 LOG.debug("Configuration Persister got {}", service);
169 LOG.debug("Context was {}", context);
170 LOG.debug("Registration was {}", registration);
172 final Thread pushingThread = new Thread(new Runnable() {
176 if(configs != null && !configs.isEmpty()) {
177 configPusher.pushConfigs(configs);
179 if(context != null) {
180 registration = context.registerService(ConfigPusher.class.getName(), configPusher, null);
181 configPusher.process(autoCloseables, platformMBeanServer, persisterAggregator);
183 LOG.warn("Unable to process configs as BundleContext is null");
185 } catch (InterruptedException e) {
186 LOG.info("ConfigPusher thread stopped",e);
188 LOG.info("Configuration Persister initialization completed.");
191 synchronized (autoCloseables) {
192 autoCloseables.add(new AutoCloseable() {
194 public void close() {
195 pushingThread.interrupt();
199 pushingThread.start();
204 public void modifiedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
205 LOG.trace("Got InnerCustomizer.modifiedService {}", reference);
209 public void removedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
210 LOG.trace("Got InnerCustomizer.removedService {}", reference);