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.lang.management.ManagementFactory;
14 import java.util.Dictionary;
15 import java.util.Hashtable;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
18 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
19 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
20 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
21 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
22 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
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.ServiceRegistration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
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 NioEventLoopGroup eventLoopGroup;
37 private HashedWheelTimer timer;
38 private ServiceRegistration<NetconfMonitoringService> regMonitoring;
41 public void start(final BundleContext context) {
43 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
44 startOperationServiceFactoryTracker(context, factoriesListener);
46 SessionIdProvider idProvider = new SessionIdProvider();
47 timer = new HashedWheelTimer();
48 long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context);
51 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
53 SessionMonitoringService monitoringService = startMonitoringService(context, factoriesListener);
55 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
56 timer, factoriesListener, idProvider, connectionTimeoutMillis, commitNot, monitoringService);
58 eventLoopGroup = new NioEventLoopGroup();
60 NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
61 serverNegotiatorFactory);
62 NetconfServerDispatcher dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
64 LocalAddress address = NetconfConfigUtil.getNetconfLocalAddress();
65 logger.trace("Starting local netconf server at {}", address);
66 dispatch.createLocalServer(address);
68 context.registerService(NetconfOperationProvider.class, factoriesListener, null);
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) {
87 logger.info("Shutting down netconf because YangStoreService service was removed");
90 eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
93 regMonitoring.unregister();
94 factoriesTracker.close();