Merge "Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX...
[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.mapping.api.NetconfOperationProvider;
13 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
14 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
15 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
16 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory;
17 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
18 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
19 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
20 import org.osgi.framework.BundleActivator;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceRegistration;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.lang.management.ManagementFactory;
27 import java.net.InetSocketAddress;
28 import java.util.Dictionary;
29 import java.util.Hashtable;
30 import java.util.concurrent.TimeUnit;
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) throws Exception {
44         InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context,
45                 "TCP is not configured, netconf not available.", false);
46
47         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
48         startOperationServiceFactoryTracker(context, factoriesListener);
49
50         SessionIdProvider idProvider = new SessionIdProvider();
51         timer = new HashedWheelTimer();
52         long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context);
53         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
54                 timer, factoriesListener, idProvider, connectionTimeoutMillis);
55
56         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
57
58         NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
59
60         NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
61                 factoriesListener, commitNot, idProvider, monitoringService);
62
63         eventLoopGroup = new NioEventLoopGroup();
64
65         NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
66                 serverNegotiatorFactory, listenerFactory);
67         NetconfServerDispatcher dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, 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
76     private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
77         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
78         factoriesTracker.open();
79     }
80
81     private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
82         NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
83         Dictionary<String, ?> dic = new Hashtable<>();
84         regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
85
86         return netconfMonitoringServiceImpl;
87     }
88
89     @Override
90     public void stop(final BundleContext context) throws Exception {
91         logger.info("Shutting down netconf because YangStoreService service was removed");
92
93         commitNot.close();
94         eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
95         timer.stop();
96
97         regMonitoring.unregister();
98         factoriesTracker.close();
99     }
100 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.