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.client.NetconfClient;
14 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
15 import org.opendaylight.controller.netconf.persist.impl.ConfigPusher;
16 import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
17 import org.opendaylight.controller.netconf.persist.impl.Util;
18 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
19 import org.osgi.framework.BundleActivator;
20 import org.osgi.framework.BundleContext;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import javax.management.MBeanServer;
25 import java.lang.management.ManagementFactory;
26 import java.net.InetSocketAddress;
27 import java.util.regex.Pattern;
28 import java.util.concurrent.TimeUnit;
30 public class ConfigPersisterActivator implements BundleActivator {
32 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
34 private final static MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
35 private static final String IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX = "ignoredMissingCapabilityRegex";
37 private static final String PUSH_TIMEOUT = "pushTimeout";
39 public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
41 public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
43 public static final String DEFAULT_IGNORED_REGEX = "^urn:ietf:params:xml:ns:netconf:base:1.0";
46 private volatile ConfigPersisterNotificationHandler jmxNotificationHandler;
47 private volatile NetconfClient netconfClient;
48 private Thread initializationThread;
49 private EventLoopGroup nettyThreadgroup;
50 private PersisterAggregator persisterAggregator;
53 public void start(final BundleContext context) throws Exception {
54 logger.debug("ConfigPersister starting");
56 PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
58 String regexProperty = propertiesProvider.getProperty(IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX);
60 if (regexProperty != null) {
61 regex = regexProperty;
63 regex = DEFAULT_IGNORED_REGEX;
66 String timeoutProperty = propertiesProvider.getProperty(PUSH_TIMEOUT);
67 long timeout = timeoutProperty == null ? ConfigPusher.DEFAULT_TIMEOUT : TimeUnit.SECONDS.toNanos(Integer.valueOf(timeoutProperty));
69 final Pattern ignoredMissingCapabilityRegex = Pattern.compile(regex);
70 nettyThreadgroup = new NioEventLoopGroup();
72 persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
73 final InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context, "Netconf is not configured, persister is not operational", true);
74 final ConfigPusher configPusher = new ConfigPusher(address, nettyThreadgroup);
77 // offload initialization to another thread in order to stop blocking activator
78 Runnable initializationRunnable = new Runnable() {
82 netconfClient = configPusher.init(persisterAggregator.loadLastConfigs());
83 jmxNotificationHandler = new ConfigPersisterNotificationHandler(
84 platformMBeanServer, netconfClient, persisterAggregator,
85 ignoredMissingCapabilityRegex);
86 jmxNotificationHandler.init();
87 } catch (InterruptedException e) {
88 Thread.currentThread().interrupt();
89 logger.error("Interrupted while waiting for netconf connection");
90 // uncaught exception handler will deal with this failure
91 throw new RuntimeException("Interrupted while waiting for netconf connection", e);
93 logger.info("Configuration Persister initialization completed.");
96 initializationThread = new Thread(initializationRunnable, "ConfigPersister-registrator");
97 initializationThread.start();
101 public void stop(BundleContext context) throws Exception {
102 initializationThread.interrupt();
103 if (jmxNotificationHandler != null) {
104 jmxNotificationHandler.close();
106 if (netconfClient != null) {
107 netconfClient = jmxNotificationHandler.getNetconfClient();
109 Util.closeClientAndDispatcher(netconfClient);
110 } catch (Exception e) {
111 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
114 nettyThreadgroup.shutdownGracefully();
115 persisterAggregator.close();