Merge "Resolve Bug:853 - remove groovy from config code generator."
[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.NetconfServerSessionNegotiatorFactory;
16 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
17 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
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 import java.util.concurrent.TimeUnit;
30
31 public class NetconfImplActivator implements BundleActivator {
32
33     private static final Logger logger = LoggerFactory.getLogger(NetconfImplActivator.class);
34
35     private NetconfOperationServiceFactoryTracker factoriesTracker;
36     private DefaultCommitNotificationProducer commitNot;
37     private NioEventLoopGroup eventLoopGroup;
38     private HashedWheelTimer timer;
39     private ServiceRegistration<NetconfMonitoringService> regMonitoring;
40
41     @Override
42     public void start(final BundleContext context)  {
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
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         NetconfServerDispatcher dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
66
67         logger.info("Starting TCP netconf server at {}", address);
68         dispatch.createServer(address);
69
70         context.registerService(NetconfOperationProvider.class, factoriesListener, null);
71
72     }
73
74     private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
75         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
76         factoriesTracker.open();
77     }
78
79     private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
80         NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
81         Dictionary<String, ?> dic = new Hashtable<>();
82         regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
83
84         return netconfMonitoringServiceImpl;
85     }
86
87     @Override
88     public void stop(final BundleContext context) {
89         logger.info("Shutting down netconf because YangStoreService service was removed");
90
91         commitNot.close();
92         eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
93         timer.stop();
94
95         regMonitoring.unregister();
96         factoriesTracker.close();
97     }
98 }