Merge "Add test for generated code checking list of dependencies."
[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
29 public class ConfigPersisterActivator implements BundleActivator {
30
31     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
32
33     private final static MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
34     private static final String IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX = "ignoredMissingCapabilityRegex";
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 volatile NetconfClient netconfClient;
45     private Thread initializationThread;
46     private EventLoopGroup nettyThreadgroup;
47     private PersisterAggregator persisterAggregator;
48
49     @Override
50     public void start(final BundleContext context) throws Exception {
51         logger.debug("ConfigPersister starting");
52
53         PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
54
55         String regexProperty = propertiesProvider.getProperty(IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX);
56         String regex;
57         if (regexProperty != null) {
58             regex = regexProperty;
59         } else {
60             regex = DEFAULT_IGNORED_REGEX;
61         }
62         final Pattern ignoredMissingCapabilityRegex = Pattern.compile(regex);
63         nettyThreadgroup = new NioEventLoopGroup();
64
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);
68
69
70         // offload initialization to another thread in order to stop blocking activator
71         Runnable initializationRunnable = new Runnable() {
72             @Override
73             public void run() {
74                 try {
75                     netconfClient = configPusher.init(persisterAggregator.loadLastConfigs());
76                     jmxNotificationHandler = new ConfigPersisterNotificationHandler(
77                             platformMBeanServer, netconfClient, persisterAggregator,
78                             ignoredMissingCapabilityRegex);
79                     jmxNotificationHandler.init();
80                 } catch (InterruptedException e) {
81                     Thread.currentThread().interrupt();
82                     logger.error("Interrupted while waiting for netconf connection");
83                     // uncaught exception handler will deal with this failure
84                     throw new RuntimeException("Interrupted while waiting for netconf connection", e);
85                 }
86             }
87         };
88         initializationThread = new Thread(initializationRunnable, "ConfigPersister-registrator");
89         initializationThread.start();
90     }
91
92     @Override
93     public void stop(BundleContext context) throws Exception {
94         initializationThread.interrupt();
95         if (jmxNotificationHandler != null) {
96             jmxNotificationHandler.close();
97         }
98         if (netconfClient != null) {
99             netconfClient = jmxNotificationHandler.getNetconfClient();
100             try {
101                 Util.closeClientAndDispatcher(netconfClient);
102             } catch (Exception e) {
103                 logger.warn("Unable to close connection to netconf {}", netconfClient, e);
104             }
105         }
106         nettyThreadgroup.shutdownGracefully();
107         persisterAggregator.close();
108     }
109 }