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;
46 import org.opendaylight.protocol.util.SSLUtil;
48 public class NetconfITSecureTest extends AbstractConfigTest {
50 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
52 private DefaultCommitNotificationProducer commitNot;
53 private NetconfServerDispatcher dispatchS;
54 private EventLoopGroup nettyThreadgroup;
58 public void setUp() throws Exception {
59 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
60 new ModuleFactory[0])));
62 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
63 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
65 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
67 nettyThreadgroup = new NioEventLoopGroup();
69 dispatchS = createDispatcher(factoriesListener);
70 ChannelFuture s = dispatchS.createServer(tlsAddress);
74 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
75 SessionIdProvider idProvider = new SessionIdProvider();
76 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
77 new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider);
79 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
80 factoriesListener, commitNot, idProvider, NetconfITTest.getNetconfMonitoringListenerService());
82 NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
83 serverNegotiatorFactory, listenerFactory);
84 return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
88 public void tearDown() throws Exception {
90 nettyThreadgroup.shutdownGracefully();
93 private SSLContext getSslContext() throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
94 IOException, UnrecoverableKeyException, KeyManagementException {
95 final InputStream keyStore = getClass().getResourceAsStream("/keystore.jks");
96 final InputStream trustStore = getClass().getResourceAsStream("/keystore.jks");
97 SSLContext sslContext = SSLUtil.initializeSecureContext("password", keyStore, trustStore, KeyManagerFactory.getDefaultAlgorithm());
103 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
104 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
105 return new HardcodedYangStoreService(yangDependencies);
108 protected List<ModuleFactory> getModuleFactories() {
109 return NetconfITTest.getModuleFactoriesS();
113 public void testSecure() throws Exception {
114 NetconfClientDispatcher dispatch = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup);
115 try (NetconfClient netconfClient = new NetconfClient("tls-client", tlsAddress, 4000, dispatch)) {