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;
30 public class NetconfImplActivator implements BundleActivator {
32 private static final Logger logger = LoggerFactory.getLogger(NetconfImplActivator.class);
34 private NetconfOperationServiceFactoryTracker factoriesTracker;
35 private DefaultCommitNotificationProducer commitNot;
36 private NetconfServerDispatcher dispatch;
37 private NioEventLoopGroup eventLoopGroup;
38 private HashedWheelTimer timer;
39 private ServiceRegistration<NetconfMonitoringService> regMonitoring;
42 public void start(final BundleContext context) throws Exception {
43 InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context,
44 "TCP is not configured, netconf not available.", false);
46 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
47 startOperationServiceFactoryTracker(context, factoriesListener);
49 SessionIdProvider idProvider = new SessionIdProvider();
50 timer = new HashedWheelTimer();
51 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
52 timer, factoriesListener, idProvider);
54 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
56 NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
58 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
59 factoriesListener, commitNot, idProvider, monitoringService);
61 eventLoopGroup = new NioEventLoopGroup();
63 NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
64 serverNegotiatorFactory, listenerFactory);
65 dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
67 logger.info("Starting TCP netconf server at {}", address);
68 dispatch.createServer(address);
72 private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
73 factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
74 factoriesTracker.open();
77 private NetconfMonitoringServiceImpl startMonitoringService(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
78 NetconfMonitoringServiceImpl netconfMonitoringServiceImpl = new NetconfMonitoringServiceImpl(factoriesListener);
79 Dictionary<String, ?> dic = new Hashtable<>();
80 regMonitoring = context.registerService(NetconfMonitoringService.class, netconfMonitoringServiceImpl, dic);
82 return netconfMonitoringServiceImpl;
86 public void stop(final BundleContext context) throws Exception {
87 logger.info("Shutting down netconf because YangStoreService service was removed");
90 eventLoopGroup.shutdownGracefully();
93 regMonitoring.unregister();
94 factoriesTracker.close();