Merge "Added test for MouontPoints and URI"
[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.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;
23
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;
29
30 public class ConfigPersisterActivator implements BundleActivator {
31
32     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
33
34     private final static MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
35     private static final String IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX = "ignoredMissingCapabilityRegex";
36
37     private static final String PUSH_TIMEOUT = "pushTimeout";
38
39     public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
40
41     public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX = "storageAdapterClass";
42
43     public static final String DEFAULT_IGNORED_REGEX = "^urn:ietf:params:xml:ns:netconf:base:1.0";
44
45
46     private volatile ConfigPersisterNotificationHandler jmxNotificationHandler;
47     private volatile NetconfClient netconfClient;
48     private Thread initializationThread;
49     private EventLoopGroup nettyThreadgroup;
50     private PersisterAggregator persisterAggregator;
51
52     @Override
53     public void start(final BundleContext context) throws Exception {
54         logger.debug("ConfigPersister starting");
55
56         PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
57
58         String regexProperty = propertiesProvider.getProperty(IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX);
59         String regex;
60         if (regexProperty != null) {
61             regex = regexProperty;
62         } else {
63             regex = DEFAULT_IGNORED_REGEX;
64         }
65
66         String timeoutProperty = propertiesProvider.getProperty(PUSH_TIMEOUT);
67         long timeout = timeoutProperty == null ? ConfigPusher.DEFAULT_TIMEOUT : TimeUnit.SECONDS.toNanos(Integer.valueOf(timeoutProperty));
68
69         final Pattern ignoredMissingCapabilityRegex = Pattern.compile(regex);
70         nettyThreadgroup = new NioEventLoopGroup();
71
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);
75
76
77         // offload initialization to another thread in order to stop blocking activator
78         Runnable initializationRunnable = new Runnable() {
79             @Override
80             public void run() {
81                 try {
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);
92                 }
93             }
94         };
95         initializationThread = new Thread(initializationRunnable, "ConfigPersister-registrator");
96         initializationThread.start();
97     }
98
99     @Override
100     public void stop(BundleContext context) throws Exception {
101         initializationThread.interrupt();
102         if (jmxNotificationHandler != null) {
103             jmxNotificationHandler.close();
104         }
105         if (netconfClient != null) {
106             netconfClient = jmxNotificationHandler.getNetconfClient();
107             try {
108                 Util.closeClientAndDispatcher(netconfClient);
109             } catch (Exception e) {
110                 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
111             }
112         }
113         nettyThreadgroup.shutdownGracefully();
114         persisterAggregator.close();
115     }
116 }