Add AuthHandler to stresstool
[controller.git] / opendaylight / netconf / netconf-testtool / src / main / java / org / opendaylight / controller / netconf / test / tool / client / stress / StressClientCallable.java
1 /*
2  * Copyright (c) 2015 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.test.tool.client.stress;
10
11 import io.netty.util.concurrent.GlobalEventExecutor;
12 import java.net.InetSocketAddress;
13 import java.util.List;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
18 import org.opendaylight.controller.netconf.client.NetconfClientSession;
19 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
20 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
21 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
22 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
23 import org.opendaylight.controller.sal.connect.api.RemoteDevice;
24 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
25 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
26 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class StressClientCallable implements Callable<Boolean>{
32
33     private static final Logger LOG = LoggerFactory.getLogger(StressClientCallable.class);
34
35     private Parameters params;
36     private final NetconfDeviceCommunicator sessionListener;
37     private final NetconfClientDispatcherImpl netconfClientDispatcher;
38     private final NetconfClientConfiguration cfg;
39     private final NetconfClientSession netconfClientSession;
40     private final ExecutionStrategy executionStrategy;
41
42     public StressClientCallable(final Parameters params,
43                                 final NetconfClientDispatcherImpl netconfClientDispatcher,
44                                 final List<NetconfMessage> preparedMessages) {
45         this.params = params;
46         this.sessionListener = getSessionListener(params.getInetAddress());
47         this.netconfClientDispatcher = netconfClientDispatcher;
48         cfg = getNetconfClientConfiguration(this.params, this.sessionListener);
49
50         LOG.info("Connecting to netconf server {}:{}", params.ip, params.port);
51         try {
52             netconfClientSession = netconfClientDispatcher.createClient(cfg).get();
53         } catch (final InterruptedException e) {
54             throw new RuntimeException(e);
55         } catch (final ExecutionException e) {
56             throw new RuntimeException("Unable to connect", e);
57         }
58         executionStrategy = getExecutionStrategy(params, preparedMessages, sessionListener);
59     }
60
61     @Override
62     public Boolean call() throws Exception {
63         executionStrategy.invoke();
64         netconfClientSession.close();
65         return true;
66     }
67
68     private static ExecutionStrategy getExecutionStrategy(final Parameters params, final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
69         if(params.async) {
70             return new AsyncExecutionStrategy(params, preparedMessages, sessionListener);
71         } else {
72             return new SyncExecutionStrategy(params, preparedMessages, sessionListener);
73         }
74     }
75
76     private static NetconfDeviceCommunicator getSessionListener(final InetSocketAddress inetAddress) {
77         final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> loggingRemoteDevice = new StressClient.LoggingRemoteDevice();
78         return new NetconfDeviceCommunicator(new RemoteDeviceId("secure-test", inetAddress), loggingRemoteDevice);
79     }
80
81     private static NetconfClientConfiguration getNetconfClientConfiguration(final Parameters params, final NetconfDeviceCommunicator sessionListener) {
82         final NetconfClientConfigurationBuilder netconfClientConfigurationBuilder = NetconfClientConfigurationBuilder.create();
83         netconfClientConfigurationBuilder.withSessionListener(sessionListener);
84         netconfClientConfigurationBuilder.withAddress(params.getInetAddress());
85         if(params.tcpHeader != null) {
86             final String header = params.tcpHeader.replaceAll("\"", "").trim() + "\n";
87             netconfClientConfigurationBuilder.withAdditionalHeader(new NetconfHelloMessageAdditionalHeader(null, null, null, null, null) {
88                 @Override
89                 public String toFormattedString() {
90                     LOG.debug("Sending TCP header {}", header);
91                     return header;
92                 }
93             });
94         }
95         netconfClientConfigurationBuilder.withProtocol(params.ssh ? NetconfClientConfiguration.NetconfClientProtocol.SSH : NetconfClientConfiguration.NetconfClientProtocol.TCP);
96         netconfClientConfigurationBuilder.withAuthHandler(new LoginPassword(params.username, params.password));
97         netconfClientConfigurationBuilder.withConnectionTimeoutMillis(20000L);
98         netconfClientConfigurationBuilder.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
99         return netconfClientConfigurationBuilder.build();
100     }
101 }