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