Bug 1970: Disabling NetconfITSecureTest test
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfITSecureTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.it;
10
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;
19
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;
62
63 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
64
65     public static final int PORT = 12024;
66     private static final InetSocketAddress TLS_ADDRESS = new InetSocketAddress("127.0.0.1", PORT);
67
68     public static final String USERNAME = "user";
69     public static final String PASSWORD = "pwd";
70
71     private NetconfSSHServer sshServer;
72
73     @Before
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());
78     }
79
80     @After
81     public void tearDown() throws Exception {
82         sshServer.close();
83         sshServer.join();
84     }
85
86     @Test
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));
93
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" +
98                     "    </get-schema>\n" +
99                     "</rpc>\n"));
100
101             response = netconfClient.sendMessage(gs);
102             assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
103                     NetconfMessageUtil.isErrorMessage(response));
104         }
105     }
106
107     /**
108      * Test all requests are handled properly and no mismatch occurs in listener
109      */
110     /* Disabled until fixed
111     @Test(timeout = 5*60*1000)
112     public void testSecureStress() throws Exception {
113         final int requests = 10000;
114
115         final NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
116         final NetconfDeviceCommunicator sessionListener = getSessionListener();
117         try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration(sessionListener))) {
118
119             final AtomicInteger responseCounter = new AtomicInteger(0);
120             final List<ListenableFuture<RpcResult<NetconfMessage>>> futures = Lists.newArrayList();
121
122             for (int i = 0; i < requests; i++) {
123                 NetconfMessage getConfig = getGetConfig();
124                 getConfig = changeMessageId(getConfig, i);
125                 final ListenableFuture<RpcResult<NetconfMessage>> netconfMessageFuture = sessionListener.sendRequest(getConfig, QName.create("namespace", "2012-12-12", "get"));
126                 futures.add(netconfMessageFuture);
127                 Futures.addCallback(netconfMessageFuture, new FutureCallback<RpcResult<NetconfMessage>>() {
128                     @Override
129                     public void onSuccess(final RpcResult<NetconfMessage> result) {
130                         responseCounter.incrementAndGet();
131                     }
132
133                     @Override
134                     public void onFailure(final Throwable t) {
135                         throw new RuntimeException(t);
136                     }
137                 });
138             }
139
140             // Wait for every future
141             for (final ListenableFuture<RpcResult<NetconfMessage>> future : futures) {
142                 try {
143                     future.get(3, TimeUnit.MINUTES);
144                 } catch (final TimeoutException e) {
145                     fail("Request " + futures.indexOf(future) + " is not responding");
146                 }
147             }
148
149             // Give future listeners some time to finish counter incrementation
150             Thread.sleep(5000);
151
152             assertEquals(requests, responseCounter.get());
153         }
154     }
155     */
156     private NetconfMessage changeMessageId(final NetconfMessage getConfig, final int i) throws IOException, SAXException {
157         String s = XmlUtil.toString(getConfig.getDocument(), false);
158         s = s.replace("101", Integer.toString(i));
159         return new NetconfMessage(XmlUtil.readXmlToDocument(s));
160     }
161
162     public NetconfClientConfiguration getClientConfiguration(final NetconfClientSessionListener sessionListener) throws IOException {
163         final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
164         b.withAddress(TLS_ADDRESS);
165         // Using session listener from sal-netconf-connector since stress test cannot be performed with simple listener
166         b.withSessionListener(sessionListener);
167         b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
168         b.withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH);
169         b.withConnectionTimeoutMillis(5000);
170         b.withAuthHandler(getAuthHandler());
171         return b.build();
172     }
173
174     @Mock
175     private RemoteDevice<NetconfSessionCapabilities, NetconfMessage> mockedRemoteDevice;
176
177     private NetconfDeviceCommunicator getSessionListener() {
178         MockitoAnnotations.initMocks(this);
179         doNothing().when(mockedRemoteDevice).onRemoteSessionUp(any(NetconfSessionCapabilities.class), any(RemoteDeviceCommunicator.class));
180         doNothing().when(mockedRemoteDevice).onRemoteSessionDown();
181         return new NetconfDeviceCommunicator(new RemoteDeviceId("secure-test"), mockedRemoteDevice);
182     }
183
184     public AuthProvider getAuthProvider() throws Exception {
185         final AuthProvider mockAuth = mock(AuthProvider.class);
186         doReturn("mockedAuth").when(mockAuth).toString();
187         doReturn(true).when(mockAuth).authenticated(anyString(), anyString());
188         return mockAuth;
189     }
190
191     public AuthenticationHandler getAuthHandler() throws IOException {
192         return new LoginPassword(USERNAME, PASSWORD);
193     }
194
195     @Override
196     protected LocalAddress getTcpServerAddress() {
197         return NetconfConfigUtil.getNetconfLocalAddress();
198     }
199 }