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