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 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;
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;
50 public class NetconfITSecureTest extends AbstractConfigTest {
52 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
54 private DefaultCommitNotificationProducer commitNot;
55 private NetconfServerDispatcher dispatchS;
56 private EventLoopGroup nettyThreadgroup;
60 public void setUp() throws Exception {
61 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
62 new ModuleFactory[0])));
64 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
65 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
67 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
69 nettyThreadgroup = new NioEventLoopGroup();
71 dispatchS = createDispatcher(Optional.of(getSslContext()), factoriesListener);
72 ChannelFuture s = dispatchS.createServer(tlsAddress);
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);
82 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
83 factoriesListener, commitNot, idProvider);
85 NetconfServerDispatcher.ServerSslChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerSslChannelInitializer(
86 sslC, serverNegotiatorFactory, listenerFactory);
87 return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
91 public void tearDown() throws Exception {
93 nettyThreadgroup.shutdownGracefully();
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());
106 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
107 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
108 return new HardcodedYangStoreService(yangDependencies);
111 protected List<ModuleFactory> getModuleFactories() {
112 return NetconfITTest.getModuleFactoriesS();
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)) {