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.controller.netconf.impl.osgi;
10 import io.netty.channel.local.LocalAddress;
11 import io.netty.channel.nio.NioEventLoopGroup;
12 import io.netty.util.HashedWheelTimer;
13 import java.util.Dictionary;
14 import java.util.Hashtable;
15 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
17 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcherImpl;
18 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
19 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
20 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactoryListener;
21 import org.opendaylight.controller.netconf.notifications.BaseNotificationPublisherRegistration;
22 import org.opendaylight.controller.netconf.notifications.NetconfNotificationCollector;
23 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
24 import org.osgi.framework.BundleActivator;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.ServiceReference;
27 import org.osgi.framework.ServiceRegistration;
28 import org.osgi.util.tracker.ServiceTracker;
29 import org.osgi.util.tracker.ServiceTrackerCustomizer;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class NetconfImplActivator implements BundleActivator {
35 private static final Logger LOG = LoggerFactory.getLogger(NetconfImplActivator.class);
37 private NetconfOperationServiceFactoryTracker factoriesTracker;
38 private NioEventLoopGroup eventLoopGroup;
39 private HashedWheelTimer timer;
40 private ServiceRegistration<NetconfMonitoringService> regMonitoring;
42 private BaseNotificationPublisherRegistration listenerReg;
45 public void start(final BundleContext context) {
47 AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory();
48 startOperationServiceFactoryTracker(context, factoriesListener);
50 SessionIdProvider idProvider = new SessionIdProvider();
51 timer = new HashedWheelTimer();
52 long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context);
54 final NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
56 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
57 timer, factoriesListener, idProvider, connectionTimeoutMillis, monitoringService);
59 eventLoopGroup = new NioEventLoopGroup();
61 NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcherImpl.ServerChannelInitializer(
62 serverNegotiatorFactory);
63 NetconfServerDispatcherImpl dispatch = new NetconfServerDispatcherImpl(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
65 LocalAddress address = NetconfConfigUtil.getNetconfLocalAddress();
66 LOG.trace("Starting local netconf server at {}", address);
67 dispatch.createLocalServer(address);
69 final ServiceTracker<NetconfNotificationCollector, NetconfNotificationCollector> notificationServiceTracker =
70 new ServiceTracker<>(context, NetconfNotificationCollector.class, new ServiceTrackerCustomizer<NetconfNotificationCollector, NetconfNotificationCollector>() {
72 public NetconfNotificationCollector addingService(ServiceReference<NetconfNotificationCollector> reference) {
73 listenerReg = context.getService(reference).registerBaseNotificationPublisher();
74 monitoringService.setNotificationPublisher(listenerReg);
79 public void modifiedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
84 public void removedService(ServiceReference<NetconfNotificationCollector> reference, NetconfNotificationCollector service) {
87 monitoringService.setNotificationPublisher(listenerReg);
90 notificationServiceTracker.open();
91 } catch (Exception e) {
92 LOG.warn("Unable to start NetconfImplActivator", e);
96 private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListener factoriesListener) {
97 factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
98 factoriesTracker.open();
101 private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, AggregatedNetconfOperationServiceFactory factoriesListener) {
102 NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
103 Dictionary<String, ?> dic = new Hashtable<>();
104 regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
106 return netconfMonitoringServiceImpl;
110 public void stop(final BundleContext context) {
111 LOG.info("Shutting down netconf because YangStoreService service was removed");
113 eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
116 regMonitoring.unregister();
117 factoriesTracker.close();