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 java.lang.management.ManagementFactory;
11 import java.net.InetSocketAddress;
12 import java.util.Dictionary;
13 import java.util.Hashtable;
14 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
17 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
18 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
19 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
20 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
21 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
22 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
23 import org.osgi.framework.BundleActivator;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.ServiceRegistration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import io.netty.channel.nio.NioEventLoopGroup;
30 import io.netty.util.HashedWheelTimer;
32 public class NetconfImplActivator implements BundleActivator {
34 private static final Logger logger = LoggerFactory.getLogger(NetconfImplActivator.class);
36 private NetconfOperationServiceFactoryTracker factoriesTracker;
37 private DefaultCommitNotificationProducer commitNot;
38 private NioEventLoopGroup eventLoopGroup;
39 private HashedWheelTimer timer;
40 private ServiceRegistration<NetconfMonitoringService> regMonitoring;
43 public void start(final BundleContext context) {
44 final InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfServerAddress(context,
45 NetconfConfigUtil.DEFAULT_NETCONF_TCP_ADDRESS);
46 final NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
47 startOperationServiceFactoryTracker(context, factoriesListener);
49 final SessionIdProvider idProvider = new SessionIdProvider();
50 timer = new HashedWheelTimer();
52 long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context);
54 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
56 SessionMonitoringService monitoringService = startMonitoringService(context, factoriesListener);
58 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
59 timer, factoriesListener, idProvider, connectionTimeoutMillis, commitNot, monitoringService);
61 eventLoopGroup = new NioEventLoopGroup();
63 NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
64 serverNegotiatorFactory);
66 NetconfServerDispatcher dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup,
69 logger.info("Starting TCP netconf server at {}", address);
70 dispatch.createServer(address);
72 context.registerService(NetconfOperationProvider.class, factoriesListener, null);
75 private void startOperationServiceFactoryTracker(final BundleContext context, final NetconfOperationServiceFactoryListenerImpl factoriesListener) {
76 factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
77 factoriesTracker.open();
80 private NetconfMonitoringServiceImpl startMonitoringService(final BundleContext context, final NetconfOperationServiceFactoryListenerImpl factoriesListener) {
81 final NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
82 final Dictionary<String, ?> dic = new Hashtable<>();
83 regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
85 return netconfMonitoringServiceImpl;
89 public void stop(final BundleContext context) {
90 logger.info("Shutting down netconf because YangStoreService service was removed");
93 eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
96 regMonitoring.unregister();
97 factoriesTracker.close();