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