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