Merge "Bring some reliability in the eclipse and maven mixed builds"
[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.channel.nio.NioEventLoopGroup;
12 import io.netty.util.HashedWheelTimer;
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     private NioEventLoopGroup eventLoopGroup;
40
41     @Override
42     public void start(final BundleContext context) throws Exception {
43         maybeTCPAddress = NetconfConfigUtil.extractTCPNetconfAddress(context);
44         maybeTLSConfiguration = NetconfConfigUtil.extractTLSConfiguration(context);
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         eventLoopGroup = new NioEventLoopGroup();
62
63         if (maybeTCPAddress.isPresent()) {
64             Optional<SSLContext> maybeSSLContext = Optional.absent();
65             InetSocketAddress address = maybeTCPAddress.get();
66             NetconfServerDispatcher.ServerSslChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerSslChannelInitializer(
67                     maybeSSLContext, serverNegotiatorFactory, listenerFactory);
68             dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
69
70             logger.info("Starting TCP netconf server at {}", address);
71             dispatch.createServer(address);
72         }
73         if (maybeTLSConfiguration.isPresent()) {
74             Optional<SSLContext> maybeSSLContext = Optional.of(maybeTLSConfiguration.get().getSslContext());
75             InetSocketAddress address = maybeTLSConfiguration.get().getAddress();
76             NetconfServerDispatcher.ServerSslChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerSslChannelInitializer(
77                     maybeSSLContext, serverNegotiatorFactory, listenerFactory);
78             dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
79
80             logger.info("Starting TLS netconf server at {}", address);
81             dispatch.createServer(address);
82         }
83     }
84
85     @Override
86     public void stop(final BundleContext context) throws Exception {
87         logger.info("Shutting down netconf because YangStoreService service was removed");
88
89         commitNot.close();
90         eventLoopGroup.shutdownGracefully();
91     }
92 }