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.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;
37 public class NetconfImplActivator implements BundleActivator {
39 private static final Logger LOG = LoggerFactory.getLogger(NetconfImplActivator.class);
41 private NetconfOperationServiceFactoryTracker factoriesTracker;
42 private NioEventLoopGroup eventLoopGroup;
43 private HashedWheelTimer timer;
44 private ServiceRegistration<NetconfMonitoringService> regMonitoring;
46 private BaseNotificationPublisherRegistration listenerReg;
49 public void start(final BundleContext context) {
51 AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory();
52 startOperationServiceFactoryTracker(context, factoriesListener);
54 SessionIdProvider idProvider = new SessionIdProvider();
55 timer = new HashedWheelTimer();
57 long connectionTimeoutMillis = NetconfConfiguration.DEFAULT_TIMEOUT_MILLIS;
59 final NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
61 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactoryBuilder()
62 .setAggregatedOpService(factoriesListener)
64 .setIdProvider(idProvider)
65 .setMonitoringService(monitoringService)
66 .setConnectionTimeoutMillis(connectionTimeoutMillis)
69 eventLoopGroup = new NioEventLoopGroup();
71 ServerChannelInitializer serverChannelInitializer = new ServerChannelInitializer(
72 serverNegotiatorFactory);
73 NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
75 LocalAddress address = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
76 LOG.trace("Starting local netconf server at {}", address);
77 dispatch.createLocalServer(address);
79 final ServiceTracker<NetconfNotificationCollector, NetconfNotificationCollector> notificationServiceTracker =
80 new ServiceTracker<>(context, NetconfNotificationCollector.class, new ServiceTrackerCustomizer<NetconfNotificationCollector, NetconfNotificationCollector>() {
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);
90 public void modifiedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
95 public void removedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
98 monitoringService.setNotificationPublisher(listenerReg);
101 notificationServiceTracker.open();
102 } catch (Exception e) {
103 LOG.warn("Unable to start NetconfImplActivator", e);
107 private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListener factoriesListener) {
108 factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
109 factoriesTracker.open();
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);
117 return netconfMonitoringServiceImpl;
121 public void stop(final BundleContext context) {
122 LOG.info("Shutting down netconf because YangStoreService service was removed");
124 eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
127 regMonitoring.unregister();
128 factoriesTracker.close();