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 static java.util.Arrays.asList;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
17 import com.google.common.collect.Lists;
18 import io.netty.channel.EventLoopGroup;
19 import io.netty.channel.nio.NioEventLoopGroup;
20 import io.netty.util.concurrent.Future;
21 import io.netty.util.concurrent.GenericFutureListener;
22 import io.netty.util.concurrent.GlobalEventExecutor;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.lang.management.ManagementFactory;
26 import java.net.InetSocketAddress;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import junit.framework.Assert;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
35 import org.opendaylight.controller.config.spi.ModuleFactory;
36 import org.opendaylight.controller.netconf.api.NetconfMessage;
37 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
38 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
39 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
40 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
41 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
42 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
43 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
44 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
45 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
46 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
47 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
48 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
49 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
50 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
51 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
52 import org.opendaylight.controller.netconf.ssh.authentication.AuthProviderImpl;
53 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
54 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
55 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
56 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
57 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
58 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
60 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
62 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
64 private DefaultCommitNotificationProducer commitNot;
65 private NetconfSSHServer sshServer;
66 private NetconfMessage getConfig;
69 public void setUp() throws Exception {
70 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
72 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, getModuleFactories().toArray(
73 new ModuleFactory[0])));
75 final NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
76 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
78 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
81 final NetconfServerDispatcher dispatchS = createDispatcher(factoriesListener);
82 dispatchS.createLocalServer(NetconfConfigUtil.getNetconfLocalAddress()).await();
83 final EventLoopGroup bossGroup = new NioEventLoopGroup();
84 sshServer = NetconfSSHServer.start(tlsAddress.getPort(), NetconfConfigUtil.getNetconfLocalAddress(), getAuthProvider(), bossGroup);
87 private NetconfServerDispatcher createDispatcher(final NetconfOperationServiceFactoryListenerImpl factoriesListener) {
88 return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
92 public void tearDown() throws Exception {
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 asList(NetconfITTest.FACTORIES);
108 public void testSecure() throws Exception {
109 final NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
110 try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration())) {
111 NetconfMessage response = netconfClient.sendMessage(getConfig);
112 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
113 NetconfMessageUtil.isErrorMessage(response));
115 final NetconfMessage gs = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"2\"\n" +
116 " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
117 " <get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
118 " <identifier>config</identifier>\n" +
122 response = netconfClient.sendMessage(gs);
123 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
124 NetconfMessageUtil.isErrorMessage(response));
129 * Test all requests are handled properly and no mismatch occurs in listener
131 @Test(timeout = 3*60*1000)
132 public void testSecureStress() throws Exception {
133 final NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
134 try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration())) {
136 final AtomicInteger responseCounter = new AtomicInteger(0);
137 final List<Future<?>> futures = Lists.newArrayList();
139 final int requests = 1000;
140 for (int i = 0; i < requests; i++) {
141 final Future<NetconfMessage> netconfMessageFuture = netconfClient.sendRequest(getConfig);
142 futures.add(netconfMessageFuture);
143 netconfMessageFuture.addListener(new GenericFutureListener<Future<? super NetconfMessage>>() {
145 public void operationComplete(final Future<? super NetconfMessage> future) throws Exception {
146 assertTrue("Request unsuccessful " + future.cause(), future.isSuccess());
147 responseCounter.incrementAndGet();
152 for (final Future<?> future : futures) {
156 // Give future listeners some time to finish counter incrementation
159 org.junit.Assert.assertEquals(requests, responseCounter.get());
163 public NetconfClientConfiguration getClientConfiguration() throws IOException {
164 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
165 b.withAddress(tlsAddress);
166 b.withSessionListener(new SimpleNetconfClientSessionListener());
167 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
168 b.withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH);
169 b.withConnectionTimeoutMillis(5000);
170 b.withAuthHandler(getAuthHandler());
174 public AuthProvider getAuthProvider() throws Exception {
175 final AuthProvider mock = mock(AuthProviderImpl.class);
176 doReturn(true).when(mock).authenticated(anyString(), anyString());
177 doReturn(PEMGenerator.generate().toCharArray()).when(mock).getPEMAsCharArray();
181 public AuthenticationHandler getAuthHandler() throws IOException {
182 return new LoginPassword("user", "pwd");