Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / AbstractNetconfConfigTest.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.it;
9
10 import java.net.InetSocketAddress;
11
12 import org.junit.After;
13 import org.junit.Before;
14 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
15 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
16 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
17 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
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.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
23 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
24 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
25
26 import io.netty.channel.EventLoopGroup;
27 import io.netty.channel.nio.NioEventLoopGroup;
28 import io.netty.util.HashedWheelTimer;
29 import io.netty.util.concurrent.GlobalEventExecutor;
30
31 public class AbstractNetconfConfigTest extends AbstractConfigTest {
32
33     private EventLoopGroup nettyThreadgroup;
34     private HashedWheelTimer hashedWheelTimer;
35
36     @Before
37     public void setUpAbstractNetconfConfigTest() {
38         nettyThreadgroup = new NioEventLoopGroup();
39         hashedWheelTimer = new HashedWheelTimer();
40     }
41
42     protected NetconfServerDispatcher createDispatcher(
43             NetconfOperationServiceFactoryListenerImpl factoriesListener, SessionMonitoringService sessionMonitoringService,
44             DefaultCommitNotificationProducer commitNotifier) {
45         SessionIdProvider idProvider = new SessionIdProvider();
46
47         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
48                 hashedWheelTimer, factoriesListener, idProvider, 5000, commitNotifier, sessionMonitoringService);
49
50         NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
51                 serverNegotiatorFactory);
52         return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
53     }
54
55     protected HashedWheelTimer getHashedWheelTimer() {
56         return hashedWheelTimer;
57     }
58
59     protected EventLoopGroup getNettyThreadgroup() {
60         return nettyThreadgroup;
61     }
62
63     @After
64     public void cleanUpTimer() {
65         hashedWheelTimer.stop();
66         nettyThreadgroup.shutdownGracefully();
67     }
68
69     public NetconfClientConfiguration getClientConfiguration(final InetSocketAddress tcpAddress, final int timeout) {
70         final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
71         b.withAddress(tcpAddress);
72         b.withSessionListener(new SimpleNetconfClientSessionListener());
73         b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
74                 timeout));
75         b.withConnectionTimeoutMillis(timeout);
76         return b.build();
77     }
78 }