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