Bug 6023 - Adress for config subsystem netconf endpoint is not configurable
[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.opendaylight.netconf.util.osgi.NetconfConfiguration;
28 import org.osgi.framework.BundleActivator;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.ServiceReference;
31 import org.osgi.framework.ServiceRegistration;
32 import org.osgi.util.tracker.ServiceTracker;
33 import org.osgi.util.tracker.ServiceTrackerCustomizer;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class NetconfImplActivator implements BundleActivator {
38
39     private static final Logger LOG = LoggerFactory.getLogger(NetconfImplActivator.class);
40
41     private NetconfOperationServiceFactoryTracker factoriesTracker;
42     private NioEventLoopGroup eventLoopGroup;
43     private HashedWheelTimer timer;
44     private ServiceRegistration<NetconfMonitoringService> regMonitoring;
45
46     private BaseNotificationPublisherRegistration listenerReg;
47
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 = new NetconfServerSessionNegotiatorFactoryBuilder()
62                     .setAggregatedOpService(factoriesListener)
63                     .setTimer(timer)
64                     .setIdProvider(idProvider)
65                     .setMonitoringService(monitoringService)
66                     .setConnectionTimeoutMillis(connectionTimeoutMillis)
67                     .build();
68
69             eventLoopGroup = new NioEventLoopGroup();
70
71             ServerChannelInitializer serverChannelInitializer = new ServerChannelInitializer(
72                     serverNegotiatorFactory);
73             NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
74
75             LocalAddress address = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
76             LOG.trace("Starting local netconf server at {}", address);
77             dispatch.createLocalServer(address);
78
79             final ServiceTracker<NetconfNotificationCollector, NetconfNotificationCollector> notificationServiceTracker =
80                     new ServiceTracker<>(context, NetconfNotificationCollector.class, new ServiceTrackerCustomizer<NetconfNotificationCollector, NetconfNotificationCollector>() {
81                         @Override
82                         public NetconfNotificationCollector addingService(ServiceReference<NetconfNotificationCollector> reference) {
83                             Preconditions.checkState(listenerReg == null, "Notification collector service was already added");
84                             listenerReg = context.getService(reference).registerBaseNotificationPublisher();
85                             monitoringService.setNotificationPublisher(listenerReg);
86                             return null;
87                         }
88
89                         @Override
90                         public void modifiedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
91
92                         }
93
94                         @Override
95                         public void removedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
96                             listenerReg.close();
97                             listenerReg = null;
98                             monitoringService.setNotificationPublisher(listenerReg);
99                         }
100                     });
101             notificationServiceTracker.open();
102         } catch (Exception e) {
103             LOG.warn("Unable to start NetconfImplActivator", e);
104         }
105     }
106
107     private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListener factoriesListener) {
108         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
109         factoriesTracker.open();
110     }
111
112     private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, AggregatedNetconfOperationServiceFactory factoriesListener) {
113         NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
114         Dictionary<String, ?> dic = new Hashtable<>();
115         regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
116
117         return netconfMonitoringServiceImpl;
118     }
119
120     @Override
121     public void stop(final BundleContext context) {
122         LOG.info("Shutting down netconf because YangStoreService service was removed");
123
124         eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
125         timer.stop();
126
127         regMonitoring.unregister();
128         factoriesTracker.close();
129     }
130 }