Fix star import and enable checkstyle rule to prevent it.
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / osgi / NetconfImplActivator.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 package org.opendaylight.controller.netconf.impl.osgi;
9
10 import com.google.common.base.Optional;
11 import io.netty.util.HashedWheelTimer;
12 import org.opendaylight.controller.config.stat.ConfigProvider;
13 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
14 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
15 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory;
16 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
17 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
18 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
19 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil.TLSConfiguration;
20 import org.osgi.framework.BundleActivator;
21 import org.osgi.framework.BundleContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import javax.net.ssl.SSLContext;
26 import java.lang.management.ManagementFactory;
27 import java.net.InetSocketAddress;
28
29 public class NetconfImplActivator implements BundleActivator {
30
31     private static final Logger logger = LoggerFactory.getLogger(NetconfImplActivator.class);
32
33     private Optional<InetSocketAddress> maybeTCPAddress;
34     private Optional<TLSConfiguration> maybeTLSConfiguration;
35
36     private NetconfOperationServiceFactoryTracker factoriesTracker;
37     private DefaultCommitNotificationProducer commitNot;
38     private NetconfServerDispatcher dispatch;
39
40     @Override
41     public void start(final BundleContext context) throws Exception {
42         final ConfigProvider configProvider = new ConfigProvider.ConfigProviderImpl(context);
43         maybeTCPAddress = NetconfConfigUtil.extractTCPNetconfAddress(configProvider);
44         maybeTLSConfiguration = NetconfConfigUtil.extractTLSConfiguration(configProvider);
45         if (maybeTCPAddress.isPresent() == false && maybeTLSConfiguration.isPresent() == false) {
46             throw new IllegalStateException("TCP nor TLS is configured, netconf not available.");
47         }
48         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
49         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
50         factoriesTracker.open();
51
52         SessionIdProvider idProvider = new SessionIdProvider();
53         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
54                 new HashedWheelTimer(), factoriesListener, idProvider);
55
56         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
57
58         NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
59                 factoriesListener, commitNot, idProvider);
60
61         if (maybeTCPAddress.isPresent()) {
62             Optional<SSLContext> maybeSSLContext = Optional.absent();
63             InetSocketAddress address = maybeTCPAddress.get();
64             dispatch = new NetconfServerDispatcher(maybeSSLContext, serverNegotiatorFactory, listenerFactory);
65
66             logger.info("Starting TCP netconf server at {}", address);
67             dispatch.createServer(address);
68         }
69         if (maybeTLSConfiguration.isPresent()) {
70             Optional<SSLContext> maybeSSLContext = Optional.of(maybeTLSConfiguration.get().getSslContext());
71             InetSocketAddress address = maybeTLSConfiguration.get().getAddress();
72             dispatch = new NetconfServerDispatcher(maybeSSLContext, serverNegotiatorFactory, listenerFactory);
73
74             logger.info("Starting TLS netconf server at {}", address);
75             dispatch.createServer(address);
76         }
77     }
78
79     @Override
80     public void stop(final BundleContext context) throws Exception {
81         logger.info("Shutting down netconf because YangStoreService service was removed");
82
83         commitNot.close();
84         dispatch.close();
85     }
86 }