Add configurable connection timeout to netconf client.
[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 io.netty.channel.nio.NioEventLoopGroup;
11 import io.netty.util.HashedWheelTimer;
12 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
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.osgi.framework.BundleActivator;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceRegistration;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import java.lang.management.ManagementFactory;
26 import java.net.InetSocketAddress;
27 import java.util.Dictionary;
28 import java.util.Hashtable;
29
30 public class NetconfImplActivator implements BundleActivator {
31
32     private static final Logger logger = LoggerFactory.getLogger(NetconfImplActivator.class);
33
34     private NetconfOperationServiceFactoryTracker factoriesTracker;
35     private DefaultCommitNotificationProducer commitNot;
36     private NetconfServerDispatcher dispatch;
37     private NioEventLoopGroup eventLoopGroup;
38     private HashedWheelTimer timer;
39     private ServiceRegistration<NetconfMonitoringService> regMonitoring;
40
41     @Override
42     public void start(final BundleContext context) throws Exception {
43         InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context,
44                 "TCP is not configured, netconf not available.", false);
45
46         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
47         startOperationServiceFactoryTracker(context, factoriesListener);
48
49         SessionIdProvider idProvider = new SessionIdProvider();
50         timer = new HashedWheelTimer();
51         long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context);
52         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
53                 timer, factoriesListener, idProvider, connectionTimeoutMillis);
54
55         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
56
57         NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
58
59         NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
60                 factoriesListener, commitNot, idProvider, monitoringService);
61
62         eventLoopGroup = new NioEventLoopGroup();
63
64         NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
65                 serverNegotiatorFactory, listenerFactory);
66         dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
67
68         logger.info("Starting TCP netconf server at {}", address);
69         dispatch.createServer(address);
70
71     }
72
73     private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
74         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
75         factoriesTracker.open();
76     }
77
78     private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
79         NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
80         Dictionary<String, ?> dic = new Hashtable<>();
81         regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
82
83         return netconfMonitoringServiceImpl;
84     }
85
86     @Override
87     public void stop(final BundleContext context) throws Exception {
88         logger.info("Shutting down netconf because YangStoreService service was removed");
89
90         commitNot.close();
91         eventLoopGroup.shutdownGracefully();
92         timer.stop();
93
94         regMonitoring.unregister();
95         factoriesTracker.close();
96     }
97 }