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