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;
29 public class ConfigPersisterActivator implements BundleActivator {
31 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
33 private final static MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
34 private static final String IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX = "ignoredMissingCapabilityRegex";
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 volatile NetconfClient netconfClient;
45 private Thread initializationThread;
46 private EventLoopGroup nettyThreadgroup;
47 private PersisterAggregator persisterAggregator;
50 public void start(final BundleContext context) throws Exception {
51 logger.debug("ConfigPersister starting");
53 PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
55 String regexProperty = propertiesProvider.getProperty(IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX);
57 if (regexProperty != null) {
58 regex = regexProperty;
60 regex = DEFAULT_IGNORED_REGEX;
62 final Pattern ignoredMissingCapabilityRegex = Pattern.compile(regex);
63 nettyThreadgroup = new NioEventLoopGroup();
65 persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
66 final InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context, "Netconf is not configured, persister is not operational", true);
67 final ConfigPusher configPusher = new ConfigPusher(address, nettyThreadgroup);
70 // offload initialization to another thread in order to stop blocking activator
71 Runnable initializationRunnable = new Runnable() {
75 netconfClient = configPusher.init(persisterAggregator.loadLastConfigs());
76 jmxNotificationHandler = new ConfigPersisterNotificationHandler(
77 platformMBeanServer, netconfClient, persisterAggregator,
78 ignoredMissingCapabilityRegex);
79 jmxNotificationHandler.init();
80 } catch (InterruptedException e) {
81 logger.info("Interrupted while waiting for netconf connection");
85 initializationThread = new Thread(initializationRunnable, "ConfigPersister-registrator");
86 initializationThread.start();
90 public void stop(BundleContext context) throws Exception {
91 initializationThread.interrupt();
92 if (jmxNotificationHandler != null) {
93 jmxNotificationHandler.close();
95 if (netconfClient != null) {
96 netconfClient = jmxNotificationHandler.getNetconfClient();
98 Util.closeClientAndDispatcher(netconfClient);
99 } catch (Exception e) {
100 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
103 nettyThreadgroup.shutdownGracefully();
104 persisterAggregator.close();