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 org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
20 import com.google.common.collect.Lists;
21 import com.google.common.util.concurrent.FutureCallback;
22 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import io.netty.channel.local.LocalAddress;
25 import io.netty.util.concurrent.GlobalEventExecutor;
26 import java.io.IOException;
27 import java.net.InetSocketAddress;
28 import java.util.List;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31 import java.util.concurrent.atomic.AtomicInteger;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.controller.netconf.api.NetconfMessage;
38 import org.opendaylight.controller.netconf.auth.AuthProvider;
39 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
40 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
41 import org.opendaylight.controller.netconf.client.NetconfClientSessionListener;
42 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
43 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
44 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
45 import org.opendaylight.controller.netconf.client.TestingNetconfClient;
46 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
47 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
48 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
49 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
50 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
51 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
52 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
53 import org.opendaylight.controller.sal.connect.api.RemoteDevice;
54 import org.opendaylight.controller.sal.connect.api.RemoteDeviceCommunicator;
55 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
56 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionCapabilities;
57 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
58 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
59 import org.opendaylight.yangtools.yang.common.QName;
60 import org.opendaylight.yangtools.yang.common.RpcResult;
61 import org.xml.sax.SAXException;
63 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
65 public static final int PORT = 12024;
66 private static final InetSocketAddress TLS_ADDRESS = new InetSocketAddress("127.0.0.1", PORT);
68 public static final String USERNAME = "user";
69 public static final String PASSWORD = "pwd";
71 private NetconfSSHServer sshServer;
74 public void setUp() throws Exception {
75 final char[] pem = PEMGenerator.generate().toCharArray();
76 sshServer = NetconfSSHServer.start(TLS_ADDRESS.getPort(), NetconfConfigUtil.getNetconfLocalAddress(), getNettyThreadgroup(), pem);
77 sshServer.setAuthProvider(getAuthProvider());
81 public void tearDown() throws Exception {
87 public void testSecure() throws Exception {
88 final NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
89 try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration(new SimpleNetconfClientSessionListener()))) {
90 NetconfMessage response = netconfClient.sendMessage(getGetConfig());
91 assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
92 NetconfMessageUtil.isErrorMessage(response));
94 final NetconfMessage gs = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"2\"\n" +
95 " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
96 " <get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
97 " <identifier>config</identifier>\n" +
101 response = netconfClient.sendMessage(gs);
102 assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
103 NetconfMessageUtil.isErrorMessage(response));
108 * Test all requests are handled properly and no mismatch occurs in listener
110 @Test(timeout = 6*60*1000)
111 public void testSecureStress() throws Exception {
112 final int requests = 4000;
114 final NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
115 final NetconfDeviceCommunicator sessionListener = getSessionListener();
116 try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration(sessionListener))) {
118 final AtomicInteger responseCounter = new AtomicInteger(0);
119 final List<ListenableFuture<RpcResult<NetconfMessage>>> futures = Lists.newArrayList();
121 for (int i = 0; i < requests; i++) {
122 NetconfMessage getConfig = getGetConfig();
123 getConfig = changeMessageId(getConfig, i);
124 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture = sessionListener.sendRequest(getConfig, QName.create("namespace", "2012-12-12", "get"));
125 futures.add(netconfMessageFuture);
126 Futures.addCallback(netconfMessageFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
128 public void onSuccess(final RpcResult<NetconfMessage> result) {
129 responseCounter.incrementAndGet();
133 public void onFailure(final Throwable t) {
134 throw new RuntimeException(t);
139 // Wait for every future
140 for (final ListenableFuture<RpcResult<NetconfMessage>> future : futures) {
142 future.get(3, TimeUnit.MINUTES);
143 } catch (final TimeoutException e) {
144 fail("Request " + futures.indexOf(future) + " is not responding");
148 // Give future listeners some time to finish counter incrementation
151 assertEquals(requests, responseCounter.get());
155 private NetconfMessage changeMessageId(final NetconfMessage getConfig, final int i) throws IOException, SAXException {
156 String s = XmlUtil.toString(getConfig.getDocument(), false);
157 s = s.replace("101", Integer.toString(i));
158 return new NetconfMessage(XmlUtil.readXmlToDocument(s));
161 public NetconfClientConfiguration getClientConfiguration(final NetconfClientSessionListener sessionListener) throws IOException {
162 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
163 b.withAddress(TLS_ADDRESS);
164 // Using session listener from sal-netconf-connector since stress test cannot be performed with simple listener
165 b.withSessionListener(sessionListener);
166 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
167 b.withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH);
168 b.withConnectionTimeoutMillis(5000);
169 b.withAuthHandler(getAuthHandler());
174 private RemoteDevice<NetconfSessionCapabilities, NetconfMessage> mockedRemoteDevice;
176 private NetconfDeviceCommunicator getSessionListener() {
177 MockitoAnnotations.initMocks(this);
178 doNothing().when(mockedRemoteDevice).onRemoteSessionUp(any(NetconfSessionCapabilities.class), any(RemoteDeviceCommunicator.class));
179 doNothing().when(mockedRemoteDevice).onRemoteSessionDown();
180 return new NetconfDeviceCommunicator(new RemoteDeviceId("secure-test"), mockedRemoteDevice);
183 public AuthProvider getAuthProvider() throws Exception {
184 final AuthProvider mockAuth = mock(AuthProvider.class);
185 doReturn("mockedAuth").when(mockAuth).toString();
186 doReturn(true).when(mockAuth).authenticated(anyString(), anyString());
190 public AuthenticationHandler getAuthHandler() throws IOException {
191 return new LoginPassword(USERNAME, PASSWORD);
195 protected LocalAddress getTcpServerAddress() {
196 return NetconfConfigUtil.getNetconfLocalAddress();