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