Merge "Add support for enums as configuration attributes in config and netconf subsys...
[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 org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
12 import org.opendaylight.controller.netconf.persist.impl.PersisterAggregator;
13 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
14 import org.osgi.framework.BundleActivator;
15 import org.osgi.framework.BundleContext;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import javax.management.MBeanServer;
20 import java.lang.management.ManagementFactory;
21 import java.net.InetSocketAddress;
22 import java.util.regex.Pattern;
23
24 public class ConfigPersisterActivator implements BundleActivator {
25
26     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterActivator.class);
27
28     private final static MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
29     private static final String IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX = "ignoredMissingCapabilityRegex";
30
31     private ConfigPersisterNotificationHandler configPersisterNotificationHandler;
32
33     private Thread initializationThread;
34
35     public static final String NETCONF_CONFIG_PERSISTER = "netconf.config.persister";
36     public static final String STORAGE_ADAPTER_CLASS_PROP_SUFFIX =  "storageAdapterClass";
37     public static final String DEFAULT_IGNORED_REGEX = "^urn:ietf:params:xml:ns:netconf:base:1.0";
38
39     @Override
40     public void start(final BundleContext context) throws Exception {
41         logger.debug("ConfigPersister starting");
42
43         PropertiesProviderBaseImpl propertiesProvider = new PropertiesProviderBaseImpl(context);
44
45         String regexProperty = propertiesProvider.getProperty(IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX);
46         String regex;
47         if (regexProperty != null) {
48             regex = regexProperty;
49         } else {
50             regex = DEFAULT_IGNORED_REGEX;
51         }
52         Pattern ignoredMissingCapabilityRegex = Pattern.compile(regex);
53         PersisterAggregator persister = PersisterAggregator.createFromProperties(propertiesProvider);
54
55         InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context,
56                 "Netconf is not configured, persister is not operational");
57         configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(persister, address,
58                 platformMBeanServer, ignoredMissingCapabilityRegex);
59
60         // offload initialization to another thread in order to stop blocking activator
61         Runnable initializationRunnable = new Runnable() {
62             @Override
63             public void run() {
64                 try {
65                     configPersisterNotificationHandler.init();
66                 } catch (InterruptedException e) {
67                     logger.info("Interrupted while waiting for netconf connection");
68                 }
69             }
70         };
71         initializationThread = new Thread(initializationRunnable, "ConfigPersister-registrator");
72         initializationThread.start();
73     }
74
75     @Override
76     public void stop(BundleContext context) throws Exception {
77         initializationThread.interrupt();
78         configPersisterNotificationHandler.close();
79     }
80 }