2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.it;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import io.netty.util.HashedWheelTimer;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.lang.management.ManagementFactory;
18 import java.net.InetSocketAddress;
19 import java.security.KeyManagementException;
20 import java.security.KeyStoreException;
21 import java.security.NoSuchAlgorithmException;
22 import java.security.UnrecoverableKeyException;
23 import java.security.cert.CertificateException;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.concurrent.TimeUnit;
27 import javax.net.ssl.KeyManagerFactory;
28 import javax.net.ssl.SSLContext;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
33 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
34 import org.opendaylight.controller.config.spi.ModuleFactory;
35 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
36 import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService;
37 import org.opendaylight.controller.netconf.client.NetconfClient;
38 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
39 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
40 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
41 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
42 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory;
43 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
44 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
45 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
47 public class NetconfITSecureTest extends AbstractConfigTest {
49 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
51 private DefaultCommitNotificationProducer commitNot;
52 private NetconfServerDispatcher dispatchS;
53 private EventLoopGroup nettyThreadgroup;
57 public void setUp() throws Exception {
58 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
59 new ModuleFactory[0])));
61 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
62 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
64 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
66 nettyThreadgroup = new NioEventLoopGroup();
68 dispatchS = createDispatcher(factoriesListener);
69 ChannelFuture s = dispatchS.createServer(tlsAddress);
73 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
74 SessionIdProvider idProvider = new SessionIdProvider();
75 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
76 new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider);
78 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
79 factoriesListener, commitNot, idProvider, NetconfITTest.getNetconfMonitoringListenerService());
81 NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
82 serverNegotiatorFactory, listenerFactory);
83 return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
87 public void tearDown() throws Exception {
89 nettyThreadgroup.shutdownGracefully();
92 private SSLContext getSslContext() throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
93 IOException, UnrecoverableKeyException, KeyManagementException {
94 final InputStream keyStore = getClass().getResourceAsStream("/keystore.jks");
95 final InputStream trustStore = getClass().getResourceAsStream("/keystore.jks");
96 SSLContext sslContext = SSLUtil.initializeSecureContext("password", keyStore, trustStore, KeyManagerFactory.getDefaultAlgorithm());
102 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
103 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
104 return new HardcodedYangStoreService(yangDependencies);
107 protected List<ModuleFactory> getModuleFactories() {
108 return NetconfITTest.getModuleFactoriesS();
112 public void testSecure() throws Exception {
113 NetconfClientDispatcher dispatch = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup);
114 try (NetconfClient netconfClient = new NetconfClient("tls-client", tlsAddress, 4000, dispatch)) {