Merge "Fix for Bug 2727 - Upgrade Akka from 2.3.4 to 2.3.9"
[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.NetconfOperationProvider;
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         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
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         SessionMonitoringService 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         context.registerService(NetconfOperationProvider.class, factoriesListener, null);
69
70     }
71
72     private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
73         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
74         factoriesTracker.open();
75     }
76
77     private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
78         NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
79         Dictionary<String, ?> dic = new Hashtable<>();
80         regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
81
82         return netconfMonitoringServiceImpl;
83     }
84
85     @Override
86     public void stop(final BundleContext context) {
87         LOG.info("Shutting down netconf because YangStoreService service was removed");
88
89         commitNot.close();
90         eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
91         timer.stop();
92
93         regMonitoring.unregister();
94         factoriesTracker.close();
95     }
96 }