Remove netconf from commons/opendaylight pom
[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.util.Dictionary;
14 import java.util.Hashtable;
15 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
17 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcherImpl;
18 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
19 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
20 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactoryListener;
21 import org.opendaylight.controller.netconf.notifications.BaseNotificationPublisherRegistration;
22 import org.opendaylight.controller.netconf.notifications.NetconfNotificationCollector;
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.ServiceReference;
27 import org.osgi.framework.ServiceRegistration;
28 import org.osgi.util.tracker.ServiceTracker;
29 import org.osgi.util.tracker.ServiceTrackerCustomizer;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class NetconfImplActivator implements BundleActivator {
34
35     private static final Logger LOG = LoggerFactory.getLogger(NetconfImplActivator.class);
36
37     private NetconfOperationServiceFactoryTracker factoriesTracker;
38     private NioEventLoopGroup eventLoopGroup;
39     private HashedWheelTimer timer;
40     private ServiceRegistration<NetconfMonitoringService> regMonitoring;
41
42     private BaseNotificationPublisherRegistration listenerReg;
43
44     @Override
45     public void start(final BundleContext context)  {
46         try {
47             AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory();
48             startOperationServiceFactoryTracker(context, factoriesListener);
49
50             SessionIdProvider idProvider = new SessionIdProvider();
51             timer = new HashedWheelTimer();
52             long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context);
53
54             final NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
55
56             NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
57                     timer, factoriesListener, idProvider, connectionTimeoutMillis, monitoringService);
58
59             eventLoopGroup = new NioEventLoopGroup();
60
61             NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcherImpl.ServerChannelInitializer(
62                     serverNegotiatorFactory);
63             NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
64
65             LocalAddress address = NetconfConfigUtil.getNetconfLocalAddress();
66             LOG.trace("Starting local netconf server at {}", address);
67             dispatch.createLocalServer(address);
68
69             final ServiceTracker<NetconfNotificationCollector, NetconfNotificationCollector> notificationServiceTracker =
70                     new ServiceTracker<>(context, NetconfNotificationCollector.class, new ServiceTrackerCustomizer<NetconfNotificationCollector, NetconfNotificationCollector>() {
71                         @Override
72                         public NetconfNotificationCollector addingService(ServiceReference<NetconfNotificationCollector> reference) {
73                             listenerReg = context.getService(reference).registerBaseNotificationPublisher();
74                             monitoringService.setNotificationPublisher(listenerReg);
75                             return null;
76                         }
77
78                         @Override
79                         public void modifiedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
80
81                         }
82
83                         @Override
84                         public void removedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
85                             listenerReg.close();
86                             listenerReg = null;
87                             monitoringService.setNotificationPublisher(listenerReg);
88                         }
89                     });
90             notificationServiceTracker.open();
91         } catch (Exception e) {
92             LOG.warn("Unable to start NetconfImplActivator", e);
93         }
94     }
95
96     private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListener factoriesListener) {
97         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
98         factoriesTracker.open();
99     }
100
101     private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, AggregatedNetconfOperationServiceFactory factoriesListener) {
102         NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
103         Dictionary<String, ?> dic = new Hashtable<>();
104         regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
105
106         return netconfMonitoringServiceImpl;
107     }
108
109     @Override
110     public void stop(final BundleContext context) {
111         LOG.info("Shutting down netconf because YangStoreService service was removed");
112
113         eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
114         timer.stop();
115
116         regMonitoring.unregister();
117         factoriesTracker.close();
118     }
119 }