Merge "1. Corrected Ip Protocol match field. 2. Added mask for Ipv6 Extension header...
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / osgi / NetconfImplActivator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.netconf.impl.osgi;
9
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;
24
25 import java.lang.management.ManagementFactory;
26 import java.net.InetSocketAddress;
27 import java.util.Dictionary;
28 import java.util.Hashtable;
29
30 public class NetconfImplActivator implements BundleActivator {
31
32     private static final Logger logger = LoggerFactory.getLogger(NetconfImplActivator.class);
33
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;
40
41     @Override
42     public void start(final BundleContext context) throws Exception {
43         InetSocketAddress address = NetconfConfigUtil.extractTCPNetconfAddress(context,
44                 "TCP is not configured, netconf not available.", false);
45
46         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
47         startOperationServiceFactoryTracker(context, factoriesListener);
48
49         SessionIdProvider idProvider = new SessionIdProvider();
50         timer = new HashedWheelTimer();
51         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
52                 timer, factoriesListener, idProvider);
53
54         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
55
56         NetconfMonitoringServiceImpl monitoringService = startMonitoringService(context, factoriesListener);
57
58         NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
59                 factoriesListener, commitNot, idProvider, monitoringService);
60
61         eventLoopGroup = new NioEventLoopGroup();
62
63         NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
64                 serverNegotiatorFactory, listenerFactory);
65         dispatch = new NetconfServerDispatcher(serverChannelInitializer, eventLoopGroup, eventLoopGroup);
66
67         logger.info("Starting TCP netconf server at {}", address);
68         dispatch.createServer(address);
69
70     }
71
72     private void startOperationServiceFactoryTracker(BundleContext context, NetconfOperationServiceFactoryListenerImpl factoriesListener) {
73         factoriesTracker = new NetconfOperationServiceFactoryTracker(context, factoriesListener);
74         factoriesTracker.open();
75     }
76
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);
81
82         return netconfMonitoringServiceImpl;
83     }
84
85     @Override
86     public void stop(final BundleContext context) throws Exception {
87         logger.info("Shutting down netconf because YangStoreService service was removed");
88
89         commitNot.close();
90         eventLoopGroup.shutdownGracefully();
91         timer.stop();
92
93         regMonitoring.unregister();
94         factoriesTracker.close();
95     }
96 }