890bbe728804e469f0d2989253e6815ec1b85d06
[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     private HashedWheelTimer timer;
41
42     @Override
43     public void start(final BundleContext context) throws Exception {
44         maybeTCPAddress = NetconfConfigUtil.extractTCPNetconfAddress(context);
45         maybeTLSConfiguration = NetconfConfigUtil.extractTLSConfiguration(context);
46         if (maybeTCPAddress.isPresent() == false && maybeTLSConfiguration.isPresent() == false) {
47             throw new IllegalStateException("TCP nor TLS is configured, netconf not available.");
48         }
49         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
50         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
51         factoriesTracker.open();
52
53         SessionIdProvider idProvider = new SessionIdProvider();
54         timer = new HashedWheelTimer();
55         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
56                 timer, factoriesListener, idProvider);
57
58         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
59
60         NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
61                 factoriesListener, commitNot, idProvider);
62
63         eventLoopGroup = new NioEventLoopGroup();
64
65         if (maybeTCPAddress.isPresent()) {
66             Optional<SSLContext> maybeSSLContext = Optional.absent();
67             InetSocketAddress address = maybeTCPAddress.get();
68             NetconfServerDispatcher.ServerSslChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerSslChannelInitializer(
69                     maybeSSLContext, serverNegotiatorFactory, listenerFactory);
70             dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
71
72             logger.info("Starting TCP netconf server at {}", address);
73             dispatch.createServer(address);
74         }
75         if (maybeTLSConfiguration.isPresent()) {
76             Optional<SSLContext> maybeSSLContext = Optional.of(maybeTLSConfiguration.get().getSslContext());
77             InetSocketAddress address = maybeTLSConfiguration.get().getAddress();
78             NetconfServerDispatcher.ServerSslChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerSslChannelInitializer(
79                     maybeSSLContext, serverNegotiatorFactory, listenerFactory);
80             dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
81
82             logger.info("Starting TLS netconf server at {}", address);
83             dispatch.createServer(address);
84         }
85     }
86
87     @Override
88     public void stop(final BundleContext context) throws Exception {
89         logger.info("Shutting down netconf because YangStoreService service was removed");
90
91         commitNot.close();
92         eventLoopGroup.shutdownGracefully();
93         timer.stop();
94     }
95 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.