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 java.lang.management.ManagementFactory;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.TimeUnit;
16 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.NetconfOperationProvider;
21 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
22 import org.opendaylight.controller.netconf.persist.impl.ConfigPusherImpl;
23 import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
24 import org.opendaylight.controller.netconf.util.CloseableUtil;
25 import org.osgi.framework.BundleActivator;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.Constants;
28 import org.osgi.framework.Filter;
29 import org.osgi.framework.InvalidSyntaxException;
30 import org.osgi.framework.ServiceReference;
31 import org.osgi.framework.ServiceRegistration;
32 import org.osgi.util.tracker.ServiceTracker;
33 import org.osgi.util.tracker.ServiceTrackerCustomizer;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.annotations.VisibleForTesting;
39 public class ConfigPersisterActivator implements BundleActivator {
41 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
42 private static final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
44 public static final String MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY = "maxWaitForCapabilitiesMillis";
45 private static final long MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT = TimeUnit.MINUTES.toMillis(2);
46 public static final String CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY = "conflictingVersionTimeoutMillis";
47 private static final long CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT = TimeUnit.SECONDS.toMillis(30);
49 public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
51 public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
53 private List<AutoCloseable> autoCloseables;
54 private volatile BundleContext context;
56 ServiceRegistration<?> registration;
59 public void start(final BundleContext context) throws Exception {
60 logger.debug("ConfigPersister starting");
61 this.context = context;
63 autoCloseables = new ArrayList<>();
64 PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
66 final PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
67 autoCloseables.add(persisterAggregator);
68 long maxWaitForCapabilitiesMillis = getMaxWaitForCapabilitiesMillis(propertiesProvider);
69 List<ConfigSnapshotHolder> configs = persisterAggregator.loadLastConfigs();
70 long conflictingVersionTimeoutMillis = getConflictingVersionTimeoutMillis(propertiesProvider);
71 logger.debug("Following configs will be pushed: {}", configs);
73 InnerCustomizer innerCustomizer = new InnerCustomizer(configs, maxWaitForCapabilitiesMillis,
74 conflictingVersionTimeoutMillis, persisterAggregator);
75 OuterCustomizer outerCustomizer = new OuterCustomizer(context, innerCustomizer);
76 new ServiceTracker<>(context, NetconfOperationProvider.class, outerCustomizer).open();
79 private long getConflictingVersionTimeoutMillis(PropertiesProviderBaseImpl propertiesProvider) {
80 String timeoutProperty = propertiesProvider.getProperty(CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY);
81 return timeoutProperty == null ? CONFLICTING_VERSION_TIMEOUT_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
84 private long getMaxWaitForCapabilitiesMillis(PropertiesProviderBaseImpl propertiesProvider) {
85 String timeoutProperty = propertiesProvider.getProperty(MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY);
86 return timeoutProperty == null ? MAX_WAIT_FOR_CAPABILITIES_MILLIS_DEFAULT : Long.valueOf(timeoutProperty);
90 public void stop(BundleContext context) throws Exception {
91 synchronized(autoCloseables) {
92 CloseableUtil.closeAll(autoCloseables);
93 if (registration != null) {
94 registration.unregister();
102 public static String getFilterString() {
104 "(" + Constants.OBJECTCLASS + "=" + NetconfOperationServiceFactory.class.getName() + ")" +
105 "(name" + "=" + "config-netconf-connector" + ")" +
109 class OuterCustomizer implements ServiceTrackerCustomizer<NetconfOperationProvider, NetconfOperationProvider> {
110 private final BundleContext context;
111 private final InnerCustomizer innerCustomizer;
113 OuterCustomizer(BundleContext context, InnerCustomizer innerCustomizer) {
114 this.context = context;
115 this.innerCustomizer = innerCustomizer;
119 public NetconfOperationProvider addingService(ServiceReference<NetconfOperationProvider> reference) {
120 logger.trace("Got OuterCustomizer.addingService {}", reference);
121 // JMX was registered, track config-netconf-connector
124 filter = context.createFilter(getFilterString());
125 } catch (InvalidSyntaxException e) {
126 throw new IllegalStateException(e);
128 new ServiceTracker<>(context, filter, innerCustomizer).open();
133 public void modifiedService(ServiceReference<NetconfOperationProvider> reference, NetconfOperationProvider service) {
138 public void removedService(ServiceReference<NetconfOperationProvider> reference, NetconfOperationProvider service) {
143 class InnerCustomizer implements ServiceTrackerCustomizer<NetconfOperationServiceFactory, NetconfOperationServiceFactory> {
144 private final List<ConfigSnapshotHolder> configs;
145 private final PersisterAggregator persisterAggregator;
146 private final long maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis;
149 InnerCustomizer(List<ConfigSnapshotHolder> configs, long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis,
150 PersisterAggregator persisterAggregator) {
151 this.configs = configs;
152 this.maxWaitForCapabilitiesMillis = maxWaitForCapabilitiesMillis;
153 this.conflictingVersionTimeoutMillis = conflictingVersionTimeoutMillis;
154 this.persisterAggregator = persisterAggregator;
158 public NetconfOperationServiceFactory addingService(ServiceReference<NetconfOperationServiceFactory> reference) {
159 logger.trace("Got InnerCustomizer.addingService {}", reference);
160 NetconfOperationServiceFactory service = reference.getBundle().getBundleContext().getService(reference);
162 logger.debug("Creating new job queue");
164 final ConfigPusherImpl configPusher = new ConfigPusherImpl(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
165 logger.debug("Configuration Persister got {}", service);
166 logger.debug("Context was {}", context);
167 logger.debug("Registration was {}", registration);
169 final Thread pushingThread = new Thread(new Runnable() {
173 if(configs != null && !configs.isEmpty()) {
174 configPusher.pushConfigs(configs);
176 if(context != null) {
177 registration = context.registerService(ConfigPusher.class.getName(), configPusher, null);
178 configPusher.process(autoCloseables, platformMBeanServer, persisterAggregator);
180 logger.warn("Unable to process configs as BundleContext is null");
182 } catch (InterruptedException e) {
183 logger.info("ConfigPusher thread stopped",e);
185 logger.info("Configuration Persister initialization completed.");
188 synchronized (autoCloseables) {
189 autoCloseables.add(new AutoCloseable() {
191 public void close() {
192 pushingThread.interrupt();
196 pushingThread.start();
201 public void modifiedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {
205 public void removedService(ServiceReference<NetconfOperationServiceFactory> reference, NetconfOperationServiceFactory service) {