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.nio.NioEventLoopGroup;
11 import io.netty.util.HashedWheelTimer;
12 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
13 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
14 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
15 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory;
16 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
17 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
18 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
19 import org.osgi.framework.BundleActivator;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceRegistration;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import java.lang.management.ManagementFactory;
26 import java.net.InetSocketAddress;
27 import java.util.Dictionary;
28 import java.util.Hashtable;
29 import java.util.concurrent.TimeUnit;
31 public class NetconfImplActivator implements BundleActivator {
33 private static final Logger logger = LoggerFactory.getLogger(NetconfImplActivator.class);
35 private NetconfOperationServiceFactoryTracker factoriesTracker;
36 private DefaultCommitNotificationProducer commitNot;
37 private NetconfServerDispatcher dispatch;
38 private NioEventLoopGroup eventLoopGroup;
39 private HashedWheelTimer timer;
40 private ServiceRegistration<NetconfMonitoringService> regMonitoring;
43 public void start(final BundleContext context) throws Exception {
44 InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context,
45 "TCP is not configured, netconf not available.", false);
47 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
48 startOperationServiceFactoryTracker(context, factoriesListener);
50 SessionIdProvider idProvider = new SessionIdProvider();
51 timer = new HashedWheelTimer();
52 long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context);
53 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
54 timer, factoriesListener, idProvider, connectionTimeoutMillis);
56 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
58 NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
60 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
61 factoriesListener, commitNot, idProvider, monitoringService);
63 eventLoopGroup = new NioEventLoopGroup();
65 NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
66 serverNegotiatorFactory, listenerFactory);
67 dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
69 logger.info("Starting TCP netconf server at {}", address);
70 dispatch.createServer(address);
74 private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
75 factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
76 factoriesTracker.open();
79 private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
80 NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
81 Dictionary<String, ?> dic = new Hashtable<>();
82 regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
84 return netconfMonitoringServiceImpl;
88 public void stop(final BundleContext context) throws Exception {
89 logger.info("Shutting down netconf because YangStoreService service was removed");
92 eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
95 regMonitoring.unregister();
96 factoriesTracker.close();