2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.impl.osgi;
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;
25 import javax.net.ssl.SSLContext;
26 import java.lang.management.ManagementFactory;
27 import java.net.InetSocketAddress;
29 public class NetconfImplActivator implements BundleActivator {
31 private static final Logger logger = LoggerFactory.getLogger(NetconfImplActivator.class);
33 private Optional<InetSocketAddress> maybeTCPAddress;
34 private Optional<TLSConfiguration> maybeTLSConfiguration;
36 private NetconfOperationServiceFactoryTracker factoriesTracker;
37 private DefaultCommitNotificationProducer commitNot;
38 private NetconfServerDispatcher dispatch;
39 private NioEventLoopGroup eventLoopGroup;
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.");
48 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
49 factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
50 factoriesTracker.open();
52 SessionIdProvider idProvider = new SessionIdProvider();
53 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
54 new HashedWheelTimer(), factoriesListener, idProvider);
56 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
58 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
59 factoriesListener, commitNot, idProvider);
61 eventLoopGroup = new NioEventLoopGroup();
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);
70 logger.info("Starting TCP netconf server at {}", address);
71 dispatch.createServer(address);
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);
80 logger.info("Starting TLS netconf server at {}", address);
81 dispatch.createServer(address);
86 public void stop(final BundleContext context) throws Exception {
87 logger.info("Shutting down netconf because YangStoreService service was removed");
90 eventLoopGroup.shutdownGracefully();