Merge "BUG-624 make netconf tcp address optional in config.ini with default value...
[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 java.lang.management.ManagementFactory;
11 import java.net.InetSocketAddress;
12 import java.util.Dictionary;
13 import java.util.Hashtable;
14 import java.util.concurrent.TimeUnit;
15
16 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
17 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
18 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
19 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
20 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
21 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
22 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
23 import org.osgi.framework.BundleActivator;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.ServiceRegistration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import io.netty.channel.nio.NioEventLoopGroup;
30 import io.netty.util.HashedWheelTimer;
31
32 public class NetconfImplActivator implements BundleActivator {
33
34     private static final Logger logger = LoggerFactory.getLogger(NetconfImplActivator.class);
35
36     private NetconfOperationServiceFactoryTracker factoriesTracker;
37     private DefaultCommitNotificationProducer commitNot;
38     private NioEventLoopGroup eventLoopGroup;
39     private HashedWheelTimer timer;
40     private ServiceRegistration<NetconfMonitoringService> regMonitoring;
41
42     @Override
43     public void start(final BundleContext context) {
44         final InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfServerAddress(context,
45                 NetconfConfigUtil.DEFAULT_NETCONF_TCP_ADDRESS);
46         final NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
47         startOperationServiceFactoryTracker(context, factoriesListener);
48
49         final SessionIdProvider idProvider = new SessionIdProvider();
50         timer = new HashedWheelTimer();
51
52         long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context);
53
54         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
55
56         SessionMonitoringService monitoringService = startMonitoringService(context, factoriesListener);
57
58         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
59                 timer, factoriesListener, idProvider, connectionTimeoutMillis, commitNot, monitoringService);
60
61         eventLoopGroup = new NioEventLoopGroup();
62
63         NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
64                 serverNegotiatorFactory);
65
66         NetconfServerDispatcher dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup,
67                 eventLoopGroup);
68
69         logger.info("Starting TCP netconf server at {}", address);
70         dispatch.createServer(address);
71
72         context.registerService(NetconfOperationProvider.class, factoriesListener, null);
73     }
74
75     private void startOperationServiceFactoryTracker(final BundleContext context, final NetconfOperationServiceFactoryListenerImpl factoriesListener) {
76         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
77         factoriesTracker.open();
78     }
79
80     private NetconfMonitoringServiceImpl startMonitoringService(final BundleContext context, final NetconfOperationServiceFactoryListenerImpl factoriesListener) {
81         final NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
82         final Dictionary<String, ?> dic = new Hashtable<>();
83         regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
84
85         return netconfMonitoringServiceImpl;
86     }
87
88     @Override
89     public void stop(final BundleContext context) {
90         logger.info("Shutting down netconf because YangStoreService service was removed");
91
92         commitNot.close();
93         eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
94         timer.stop();
95
96         regMonitoring.unregister();
97         factoriesTracker.close();
98     }
99 }