Finish bumping to yangtools 2.1.8
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / 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.netconf.impl.osgi;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.channel.local.LocalAddress;
12 import io.netty.channel.nio.NioEventLoopGroup;
13 import io.netty.util.HashedWheelTimer;
14 import java.util.Dictionary;
15 import java.util.Hashtable;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
18 import org.opendaylight.netconf.impl.NetconfServerDispatcherImpl;
19 import org.opendaylight.netconf.impl.NetconfServerDispatcherImpl.ServerChannelInitializer;
20 import org.opendaylight.netconf.impl.NetconfServerSessionNegotiatorFactory;
21 import org.opendaylight.netconf.impl.NetconfServerSessionNegotiatorFactoryBuilder;
22 import org.opendaylight.netconf.impl.SessionIdProvider;
23 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
24 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
25 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
26 import org.opendaylight.netconf.util.NetconfConfiguration;
27 import org.osgi.framework.BundleActivator;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceReference;
30 import org.osgi.framework.ServiceRegistration;
31 import org.osgi.util.tracker.ServiceTracker;
32 import org.osgi.util.tracker.ServiceTrackerCustomizer;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class NetconfImplActivator implements BundleActivator {
37
38     private static final Logger LOG = LoggerFactory.getLogger(NetconfImplActivator.class);
39
40     private NetconfOperationServiceFactoryTracker factoriesTracker;
41     private NioEventLoopGroup eventLoopGroup;
42     private HashedWheelTimer timer;
43     private ServiceRegistration<NetconfMonitoringService> regMonitoring;
44
45     private BaseNotificationPublisherRegistration listenerReg;
46
47     @SuppressWarnings("checkstyle:IllegalCatch")
48     @Override
49     public void start(final BundleContext context) {
50         try {
51             AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory();
52             startOperationServiceFactoryTracker(context, factoriesListener);
53
54             SessionIdProvider idProvider = new SessionIdProvider();
55             timer = new HashedWheelTimer();
56
57             long connectionTimeoutMillis = NetconfConfiguration.DEFAULT_TIMEOUT_MILLIS;
58
59             final NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
60
61             NetconfServerSessionNegotiatorFactory serverNegotiatorFactory =
62                     new NetconfServerSessionNegotiatorFactoryBuilder()
63                             .setAggregatedOpService(factoriesListener)
64                             .setTimer(timer)
65                             .setIdProvider(idProvider)
66                             .setMonitoringService(monitoringService)
67                             .setConnectionTimeoutMillis(connectionTimeoutMillis)
68                             .build();
69
70             eventLoopGroup = new NioEventLoopGroup();
71
72             ServerChannelInitializer serverChannelInitializer = new ServerChannelInitializer(
73                     serverNegotiatorFactory);
74             NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer,
75                     eventLoopGroup, eventLoopGroup);
76
77             LocalAddress address = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
78             LOG.trace("Starting local netconf server at {}", address);
79             dispatch.createLocalServer(address);
80
81             final ServiceTracker<NetconfNotificationCollector, NetconfNotificationCollector>
82                     notificationServiceTracker = new ServiceTracker<>(context, NetconfNotificationCollector.class,
83                     new ServiceTrackerCustomizer<NetconfNotificationCollector, NetconfNotificationCollector>() {
84                             @Override
85                             public NetconfNotificationCollector addingService(ServiceReference<
86                                     NetconfNotificationCollector> reference) {
87                                 Preconditions.checkState(listenerReg == null,
88                                         "Notification collector service was already added");
89                                 listenerReg = context.getService(reference).registerBaseNotificationPublisher();
90                                 monitoringService.setNotificationPublisher(listenerReg);
91                                 return null;
92                             }
93
94                             @Override
95                             public void modifiedService(ServiceReference<NetconfNotificationCollector> reference,
96                                             NetconfNotificationCollector service) {
97
98                                 }
99
100                             @Override
101                             public void removedService(ServiceReference<NetconfNotificationCollector> reference,
102                                            NetconfNotificationCollector service) {
103                                 listenerReg.close();
104                                 listenerReg = null;
105                                 monitoringService.setNotificationPublisher(listenerReg);
106                             }
107                         });
108             notificationServiceTracker.open();
109         } catch (Exception e) {
110             LOG.warn("Unable to start NetconfImplActivator", e);
111         }
112     }
113
114     private void startOperationServiceFactoryTracker(BundleContext context,
115                                                      NetconfOperationServiceFactoryListener factoriesListener) {
116         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
117         factoriesTracker.open();
118     }
119
120     private NetconfMonitoringServiceImpl startMonitoringService(
121             BundleContext context,
122             AggregatedNetconfOperationServiceFactory factoriesListener) {
123         NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
124         Dictionary<String, ?> dic = new Hashtable<>();
125         regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
126
127         return netconfMonitoringServiceImpl;
128     }
129
130     @Override
131     public void stop(final BundleContext context) {
132         LOG.info("Shutting down netconf because YangStoreService service was removed");
133
134         eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
135         timer.stop();
136
137         regMonitoring.unregister();
138         factoriesTracker.close();
139     }
140 }