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 com.google.common.base.Optional;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
16 import org.opendaylight.controller.netconf.persist.impl.ConfigPusher;
17 import org.opendaylight.controller.netconf.persist.impl.ConfigPusherConfiguration;
18 import org.opendaylight.controller.netconf.persist.impl.ConfigPusherConfigurationBuilder;
19 import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
20 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
21 import org.osgi.framework.BundleActivator;
22 import org.osgi.framework.BundleContext;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import javax.management.MBeanServer;
27 import java.lang.management.ManagementFactory;
28 import java.net.InetSocketAddress;
29 import java.util.concurrent.ThreadFactory;
30 import java.util.regex.Pattern;
32 public class ConfigPersisterActivator implements BundleActivator {
34 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
36 public static final String IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX = "ignoredMissingCapabilityRegex";
38 public static final String MAX_WAIT_FOR_CAPABILITIES_MILLIS = "maxWaitForCapabilitiesMillis";
40 public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
42 public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
44 public static final String DEFAULT_IGNORED_REGEX = "^urn:ietf:params:xml:ns:netconf:base:1.0";
46 private final MBeanServer platformMBeanServer;
48 private final Optional<ConfigPusherConfiguration> initialConfigForPusher;
49 private volatile ConfigPersisterNotificationHandler jmxNotificationHandler;
50 private Thread initializationThread;
51 private ThreadFactory initializationThreadFactory;
52 private EventLoopGroup nettyThreadGroup;
53 private PersisterAggregator persisterAggregator;
55 public ConfigPersisterActivator() {
56 this(new ThreadFactory() {
58 public Thread newThread(Runnable initializationRunnable) {
59 return new Thread(initializationRunnable, "ConfigPersister-registrator");
61 }, ManagementFactory.getPlatformMBeanServer(), null);
65 protected ConfigPersisterActivator(ThreadFactory threadFactory, MBeanServer mBeanServer,
66 ConfigPusherConfiguration initialConfigForPusher) {
67 this.initializationThreadFactory = threadFactory;
68 this.platformMBeanServer = mBeanServer;
69 this.initialConfigForPusher = Optional.fromNullable(initialConfigForPusher);
73 public void start(final BundleContext context) throws Exception {
74 logger.debug("ConfigPersister starting");
76 PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
78 final Pattern ignoredMissingCapabilityRegex = getIgnoredCapabilitiesProperty(propertiesProvider);
80 persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
82 final ConfigPusher configPusher = new ConfigPusher(getConfigurationForPusher(context, propertiesProvider));
84 // offload initialization to another thread in order to stop blocking activator
85 Runnable initializationRunnable = new Runnable() {
89 configPusher.pushConfigs(persisterAggregator.loadLastConfigs());
90 jmxNotificationHandler = new ConfigPersisterNotificationHandler(platformMBeanServer, persisterAggregator,
91 ignoredMissingCapabilityRegex);
92 } catch (InterruptedException e) {
93 Thread.currentThread().interrupt();
94 logger.error("Interrupted while waiting for netconf connection");
95 // uncaught exception handler will deal with this failure
96 throw new RuntimeException("Interrupted while waiting for netconf connection", e);
98 logger.info("Configuration Persister initialization completed.");
102 initializationThread = initializationThreadFactory.newThread(initializationRunnable);
103 initializationThread.start();
106 private Pattern getIgnoredCapabilitiesProperty(PropertiesProviderBaseImpl propertiesProvider) {
107 String regexProperty = propertiesProvider.getProperty(IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX);
109 if (regexProperty != null) {
110 regex = regexProperty;
112 regex = DEFAULT_IGNORED_REGEX;
114 return Pattern.compile(regex);
117 private Optional<Long> getMaxWaitForCapabilitiesProperty(PropertiesProviderBaseImpl propertiesProvider) {
118 String timeoutProperty = propertiesProvider.getProperty(MAX_WAIT_FOR_CAPABILITIES_MILLIS);
119 return Optional.fromNullable(timeoutProperty == null ? null : Long.valueOf(timeoutProperty));
122 private ConfigPusherConfiguration getConfigurationForPusher(BundleContext context,
123 PropertiesProviderBaseImpl propertiesProvider) {
125 // If configuration was injected via constructor, use it
126 if(initialConfigForPusher.isPresent())
127 return initialConfigForPusher.get();
129 Optional<Long> maxWaitForCapabilitiesMillis = getMaxWaitForCapabilitiesProperty(propertiesProvider);
130 final InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context,
131 "Netconf is not configured, persister is not operational", true);
133 nettyThreadGroup = new NioEventLoopGroup();
135 ConfigPusherConfigurationBuilder configPusherConfigurationBuilder = ConfigPusherConfigurationBuilder.aConfigPusherConfiguration();
137 if(maxWaitForCapabilitiesMillis.isPresent())
138 configPusherConfigurationBuilder.withNetconfCapabilitiesWaitTimeoutMs(maxWaitForCapabilitiesMillis.get());
140 return configPusherConfigurationBuilder
141 .withEventLoopGroup(nettyThreadGroup)
142 .withNetconfAddress(address)
147 public void stop(BundleContext context) throws Exception {
148 initializationThread.interrupt();
149 if (jmxNotificationHandler != null) {
150 jmxNotificationHandler.close();
152 if(nettyThreadGroup!=null)
153 nettyThreadGroup.shutdownGracefully();
154 persisterAggregator.close();