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