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