Add configurable connection timeout to netconf client.
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfITSecureTest.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
9 package org.opendaylight.controller.netconf.it;
10
11 import io.netty.channel.ChannelFuture;
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
16 import org.opendaylight.controller.config.spi.ModuleFactory;
17 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
18 import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService;
19 import org.opendaylight.controller.netconf.client.NetconfClient;
20 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
21 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
22 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
23 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
24 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
25
26 import javax.net.ssl.KeyManagerFactory;
27 import javax.net.ssl.SSLContext;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.lang.management.ManagementFactory;
31 import java.net.InetSocketAddress;
32 import java.security.KeyManagementException;
33 import java.security.KeyStoreException;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.UnrecoverableKeyException;
36 import java.security.cert.CertificateException;
37 import java.util.Collection;
38 import java.util.List;
39
40 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
41
42     private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
43
44     private DefaultCommitNotificationProducer commitNot;
45     private NetconfServerDispatcher dispatchS;
46
47
48     @Before
49     public void setUp() throws Exception {
50         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
51                 new ModuleFactory[0])));
52
53         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
54         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
55
56         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
57
58
59         dispatchS = createDispatcher(factoriesListener);
60         ChannelFuture s = dispatchS.createServer(tlsAddress);
61         s.await();
62     }
63
64     private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
65         return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
66     }
67
68     @After
69     public void tearDown() throws Exception {
70         commitNot.close();
71     }
72
73     private SSLContext getSslContext() throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
74             IOException, UnrecoverableKeyException, KeyManagementException {
75         final InputStream keyStore = getClass().getResourceAsStream("/keystore.jks");
76         final InputStream trustStore = getClass().getResourceAsStream("/keystore.jks");
77         SSLContext sslContext = SSLUtil.initializeSecureContext("password", keyStore, trustStore, KeyManagerFactory.getDefaultAlgorithm());
78         keyStore.close();
79         trustStore.close();
80         return sslContext;
81     }
82
83     private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
84         final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
85         return new HardcodedYangStoreService(yangDependencies);
86     }
87
88     protected List<ModuleFactory> getModuleFactories() {
89         return NetconfITTest.getModuleFactoriesS();
90     }
91
92     @Test
93     public void testSecure() throws Exception {
94         NetconfClientDispatcher dispatch = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000);
95         try (NetconfClient netconfClient = new NetconfClient("tls-client", tlsAddress, 4000, dispatch))  {
96
97         }
98     }
99 }