Merge "Handle uncaught exceptions from Clustering Services in HostTracker"
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / osgi / ConfigPersisterActivator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.persist.impl.osgi;
10
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;
21
22 import javax.management.MBeanServer;
23 import java.lang.management.ManagementFactory;
24 import java.net.InetSocketAddress;
25 import java.util.regex.Pattern;
26
27 public class ConfigPersisterActivator implements BundleActivator {
28
29     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
30
31     private final static MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
32     private static final String IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX = "ignoredMissingCapabilityRegex";
33
34     private static final String MAX_WAIT_FOR_CAPABILITIES_MILLIS = "maxWaitForCapabilitiesMillis";
35
36     public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
37
38     public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
39
40     public static final String DEFAULT_IGNORED_REGEX = "^urn:ietf:params:xml:ns:netconf:base:1.0";
41
42
43     private volatile ConfigPersisterNotificationHandler jmxNotificationHandler;
44     private Thread initializationThread;
45     private EventLoopGroup nettyThreadGroup;
46     private PersisterAggregator persisterAggregator;
47
48     @Override
49     public void start(final BundleContext context) throws Exception {
50         logger.debug("ConfigPersister starting");
51
52         PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
53
54         String regexProperty = propertiesProvider.getProperty(IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX);
55         String regex;
56         if (regexProperty != null) {
57             regex = regexProperty;
58         } else {
59             regex = DEFAULT_IGNORED_REGEX;
60         }
61
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;
66         } else {
67             maxWaitForCapabilitiesMillis = Integer.valueOf(timeoutProperty);
68         }
69
70         final Pattern ignoredMissingCapabilityRegex = Pattern.compile(regex);
71         nettyThreadGroup = new NioEventLoopGroup();
72
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);
78
79
80         // offload initialization to another thread in order to stop blocking activator
81         Runnable initializationRunnable = new Runnable() {
82             @Override
83             public void run() {
84                 try {
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);
93                 }
94                 logger.info("Configuration Persister initialization completed.");
95             }
96         };
97         initializationThread = new Thread(initializationRunnable, "ConfigPersister-registrator");
98         initializationThread.start();
99     }
100
101     @Override
102     public void stop(BundleContext context) throws Exception {
103         initializationThread.interrupt();
104         if (jmxNotificationHandler != null) {
105             jmxNotificationHandler.close();
106         }
107         nettyThreadGroup.shutdownGracefully();
108         persisterAggregator.close();
109     }
110 }