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.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;
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;
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;
56 public void setUp() throws Exception {
57 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
58 new ModuleFactory[0])));
60 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
61 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
63 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
65 dispatchS = createDispatcher(Optional.of(getSslContext()), factoriesListener);
66 ChannelFuture s = dispatchS.createServer(tlsAddress);
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);
76 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
77 factoriesListener, commitNot, idProvider);
79 return new NetconfServerDispatcher(sslC, serverNegotiatorFactory, listenerFactory);
83 public void tearDown() throws Exception {
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());
98 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
99 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
100 return new HardcodedYangStoreService(yangDependencies);
103 protected List<ModuleFactory> getModuleFactories() {
104 return NetconfITTest.getModuleFactoriesS();
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)) {