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 org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
16 import org.opendaylight.controller.config.spi.ModuleFactory;
17 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
18 import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService;
19 import org.opendaylight.controller.netconf.client.NetconfClient;
20 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
21 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
22 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
23 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
24 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
26 import javax.net.ssl.KeyManagerFactory;
27 import javax.net.ssl.SSLContext;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.lang.management.ManagementFactory;
31 import java.net.InetSocketAddress;
32 import java.security.KeyManagementException;
33 import java.security.KeyStoreException;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.UnrecoverableKeyException;
36 import java.security.cert.CertificateException;
37 import java.util.Collection;
38 import java.util.List;
40 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
42 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
44 private DefaultCommitNotificationProducer commitNot;
45 private NetconfServerDispatcher dispatchS;
49 public void setUp() throws Exception {
50 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
51 new ModuleFactory[0])));
53 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
54 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
56 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
59 dispatchS = createDispatcher(factoriesListener);
60 ChannelFuture s = dispatchS.createServer(tlsAddress);
64 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
65 return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
69 public void tearDown() throws Exception {
73 private SSLContext getSslContext() throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
74 IOException, UnrecoverableKeyException, KeyManagementException {
75 final InputStream keyStore = getClass().getResourceAsStream("/keystore.jks");
76 final InputStream trustStore = getClass().getResourceAsStream("/keystore.jks");
77 SSLContext sslContext = SSLUtil.initializeSecureContext("password", keyStore, trustStore, KeyManagerFactory.getDefaultAlgorithm());
83 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
84 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
85 return new HardcodedYangStoreService(yangDependencies);
88 protected List<ModuleFactory> getModuleFactories() {
89 return NetconfITTest.getModuleFactoriesS();
93 public void testSecure() throws Exception {
94 NetconfClientDispatcher dispatch = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000);
95 try (NetconfClient netconfClient = new NetconfClient("tls-client", tlsAddress, 4000, dispatch)) {