cf1b0af454905d8aa6091085f3f3d538d3dd9029
[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 com.google.common.base.Optional;
12 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
13 import org.opendaylight.controller.netconf.persist.impl.NoOpStorageAdapter;
14 import org.opendaylight.controller.netconf.persist.impl.PersisterImpl;
15 import org.opendaylight.controller.config.stat.ConfigProvider;
16 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
17 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.TLSConfiguration;
18 import org.osgi.framework.BundleActivator;
19 import org.osgi.framework.BundleContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import javax.management.MBeanServer;
24 import java.lang.management.ManagementFactory;
25 import java.net.InetSocketAddress;
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
33     private ConfigPersisterNotificationHandler configPersisterNotificationHandler;
34
35     private Thread initializationThread;
36
37     @Override
38     public void start(BundleContext context) throws Exception {
39         logger.debug("ConfigPersister activator started");
40
41         ConfigProvider configProvider = new ConfigProvider.ConfigProviderImpl(context);
42         Optional<PersisterImpl> maybePersister = PersisterImpl.createFromProperties(configProvider);
43         if (maybePersister.isPresent() == false) {
44             throw new IllegalStateException("No persister is defined in " + PersisterImpl.STORAGE_ADAPTER_CLASS_PROP
45                     + " property. For noop persister use " + NoOpStorageAdapter.class.getCanonicalName()
46                     + " . Persister is not operational");
47         }
48
49         Optional<TLSConfiguration> maybeTLSConfiguration = NetconfConfigUtil.extractTLSConfiguration(configProvider);
50         Optional<InetSocketAddress> maybeTCPAddress = NetconfConfigUtil.extractTCPNetconfAddress(configProvider);
51
52         InetSocketAddress address;
53         if (maybeTLSConfiguration.isPresent()) {
54             throw new UnsupportedOperationException("TLS is currently not supported for persister");
55         } else if (maybeTCPAddress.isPresent()) {
56             address = maybeTCPAddress.get();
57         } else {
58             throw new IllegalStateException("Netconf is not configured, persister is not operational");
59         }
60
61         PersisterImpl persister = maybePersister.get();
62         configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(persister, address,
63                 platformMBeanServer);
64         Runnable initializationRunnable = new Runnable() {
65             @Override
66             public void run() {
67                 try {
68                     configPersisterNotificationHandler.init();
69                 } catch (InterruptedException e) {
70                     logger.info("Interrupted while waiting for netconf connection");
71                 }
72             }
73         };
74         initializationThread = new Thread(initializationRunnable, "ConfigPersister-registrator");
75         initializationThread.start();
76     }
77
78     @Override
79     public void stop(BundleContext context) throws Exception {
80         initializationThread.interrupt();
81         configPersisterNotificationHandler.close();
82     }
83 }