Merge "caching the same capabilities for devices using Interner."
[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.osgi.NetconfConfigUtil;
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     @Override
48     public void start(final BundleContext context) {
49         try {
50             AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory();
51             startOperationServiceFactoryTracker(context, factoriesListener);
52
53             SessionIdProvider idProvider = new SessionIdProvider();
54             timer = new HashedWheelTimer();
55             long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context);
56
57             final NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
58
59             NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactoryBuilder()
60                     .setAggregatedOpService(factoriesListener)
61                     .setTimer(timer)
62                     .setIdProvider(idProvider)
63                     .setMonitoringService(monitoringService)
64                     .setConnectionTimeoutMillis(connectionTimeoutMillis)
65                     .build();
66
67             eventLoopGroup = new NioEventLoopGroup();
68
69             ServerChannelInitializer serverChannelInitializer = new ServerChannelInitializer(
70                     serverNegotiatorFactory);
71             NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
72
73             LocalAddress address = NetconfConfigUtil.getNetconfLocalAddress();
74             LOG.trace("Starting local netconf server at {}", address);
75             dispatch.createLocalServer(address);
76
77             final ServiceTracker<NetconfNotificationCollector, NetconfNotificationCollector> notificationServiceTracker =
78                     new ServiceTracker<>(context, NetconfNotificationCollector.class, new ServiceTrackerCustomizer<NetconfNotificationCollector, NetconfNotificationCollector>() {
79                         @Override
80                         public NetconfNotificationCollector addingService(ServiceReference<NetconfNotificationCollector> reference) {
81                             Preconditions.checkState(listenerReg == null, "Notification collector service was already added");
82                             listenerReg = context.getService(reference).registerBaseNotificationPublisher();
83                             monitoringService.setNotificationPublisher(listenerReg);
84                             return null;
85                         }
86
87                         @Override
88                         public void modifiedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
89
90                         }
91
92                         @Override
93                         public void removedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
94                             listenerReg.close();
95                             listenerReg = null;
96                             monitoringService.setNotificationPublisher(listenerReg);
97                         }
98                     });
99             notificationServiceTracker.open();
100         } catch (Exception e) {
101             LOG.warn("Unable to start NetconfImplActivator", e);
102         }
103     }
104
105     private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListener factoriesListener) {
106         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
107         factoriesTracker.open();
108     }
109
110     private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, AggregatedNetconfOperationServiceFactory factoriesListener) {
111         NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
112         Dictionary<String, ?> dic = new Hashtable<>();
113         regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
114
115         return netconfMonitoringServiceImpl;
116     }
117
118     @Override
119     public void stop(final BundleContext context) {
120         LOG.info("Shutting down netconf because YangStoreService service was removed");
121
122         eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
123         timer.stop();
124
125         regMonitoring.unregister();
126         factoriesTracker.close();
127     }
128 }