2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.impl.osgi;
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.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;
36 public class NetconfImplActivator implements BundleActivator {
38 private static final Logger LOG = LoggerFactory.getLogger(NetconfImplActivator.class);
40 private NetconfOperationServiceFactoryTracker factoriesTracker;
41 private NioEventLoopGroup eventLoopGroup;
42 private HashedWheelTimer timer;
43 private ServiceRegistration<NetconfMonitoringService> regMonitoring;
45 private BaseNotificationPublisherRegistration listenerReg;
48 public void start(final BundleContext context) {
50 AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory();
51 startOperationServiceFactoryTracker(context, factoriesListener);
53 SessionIdProvider idProvider = new SessionIdProvider();
54 timer = new HashedWheelTimer();
56 long connectionTimeoutMillis = NetconfConfiguration.DEFAULT_TIMEOUT_MILLIS;
58 final NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
60 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactoryBuilder()
61 .setAggregatedOpService(factoriesListener)
63 .setIdProvider(idProvider)
64 .setMonitoringService(monitoringService)
65 .setConnectionTimeoutMillis(connectionTimeoutMillis)
68 eventLoopGroup = new NioEventLoopGroup();
70 ServerChannelInitializer serverChannelInitializer = new ServerChannelInitializer(
71 serverNegotiatorFactory);
72 NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
74 LocalAddress address = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
75 LOG.trace("Starting local netconf server at {}", address);
76 dispatch.createLocalServer(address);
78 final ServiceTracker<NetconfNotificationCollector, NetconfNotificationCollector> notificationServiceTracker =
79 new ServiceTracker<>(context, NetconfNotificationCollector.class, new ServiceTrackerCustomizer<NetconfNotificationCollector, NetconfNotificationCollector>() {
81 public NetconfNotificationCollector addingService(ServiceReference<NetconfNotificationCollector> reference) {
82 Preconditions.checkState(listenerReg == null, "Notification collector service was already added");
83 listenerReg = context.getService(reference).registerBaseNotificationPublisher();
84 monitoringService.setNotificationPublisher(listenerReg);
89 public void modifiedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
94 public void removedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
97 monitoringService.setNotificationPublisher(listenerReg);
100 notificationServiceTracker.open();
101 } catch (Exception e) {
102 LOG.warn("Unable to start NetconfImplActivator", e);
106 private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListener factoriesListener) {
107 factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
108 factoriesTracker.open();
111 private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, AggregatedNetconfOperationServiceFactory factoriesListener) {
112 NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
113 Dictionary<String, ?> dic = new Hashtable<>();
114 regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
116 return netconfMonitoringServiceImpl;
120 public void stop(final BundleContext context) {
121 LOG.info("Shutting down netconf because YangStoreService service was removed");
123 eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
126 regMonitoring.unregister();
127 factoriesTracker.close();