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