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 io.netty.channel.EventLoopGroup;
12 import io.netty.channel.nio.NioEventLoopGroup;
13 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
14 import org.opendaylight.controller.netconf.persist.impl.ConfigPusher;
15 import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
16 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
17 import org.osgi.framework.BundleActivator;
18 import org.osgi.framework.BundleContext;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import javax.management.MBeanServer;
23 import java.lang.management.ManagementFactory;
24 import java.net.InetSocketAddress;
25 import java.util.regex.Pattern;
27 public class ConfigPersisterActivator implements BundleActivator {
29 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
31 private final static MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
32 private static final String IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX = "ignoredMissingCapabilityRegex";
34 private static final String MAX_WAIT_FOR_CAPABILITIES_MILLIS = "maxWaitForCapabilitiesMillis";
36 public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
38 public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
40 public static final String DEFAULT_IGNORED_REGEX = "^urn:ietf:params:xml:ns:netconf:base:1.0";
43 private volatile ConfigPersisterNotificationHandler jmxNotificationHandler;
44 private Thread initializationThread;
45 private EventLoopGroup nettyThreadGroup;
46 private PersisterAggregator persisterAggregator;
49 public void start(final BundleContext context) throws Exception {
50 logger.debug("ConfigPersister starting");
52 PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
54 String regexProperty = propertiesProvider.getProperty(IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX);
56 if (regexProperty != null) {
57 regex = regexProperty;
59 regex = DEFAULT_IGNORED_REGEX;
62 String timeoutProperty = propertiesProvider.getProperty(MAX_WAIT_FOR_CAPABILITIES_MILLIS);
63 long maxWaitForCapabilitiesMillis;
64 if (timeoutProperty == null) {
65 maxWaitForCapabilitiesMillis = ConfigPusher.DEFAULT_MAX_WAIT_FOR_CAPABILITIES_MILLIS;
67 maxWaitForCapabilitiesMillis = Integer.valueOf(timeoutProperty);
70 final Pattern ignoredMissingCapabilityRegex = Pattern.compile(regex);
71 nettyThreadGroup = new NioEventLoopGroup();
73 persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
74 final InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context,
75 "Netconf is not configured, persister is not operational", true);
76 final ConfigPusher configPusher = new ConfigPusher(address, nettyThreadGroup, maxWaitForCapabilitiesMillis,
77 ConfigPusher.DEFAULT_CONNECTION_TIMEOUT_MILLIS);
80 // offload initialization to another thread in order to stop blocking activator
81 Runnable initializationRunnable = new Runnable() {
85 configPusher.pushConfigs(persisterAggregator.loadLastConfigs());
86 jmxNotificationHandler = new ConfigPersisterNotificationHandler(platformMBeanServer, persisterAggregator,
87 ignoredMissingCapabilityRegex);
88 } catch (InterruptedException e) {
89 Thread.currentThread().interrupt();
90 logger.error("Interrupted while waiting for netconf connection");
91 // uncaught exception handler will deal with this failure
92 throw new RuntimeException("Interrupted while waiting for netconf connection", e);
94 logger.info("Configuration Persister initialization completed.");
97 initializationThread = new Thread(initializationRunnable, "ConfigPersister-registrator");
98 initializationThread.start();
102 public void stop(BundleContext context) throws Exception {
103 initializationThread.interrupt();
104 if (jmxNotificationHandler != null) {
105 jmxNotificationHandler.close();
107 nettyThreadGroup.shutdownGracefully();
108 persisterAggregator.close();