d808f2bf5d6802ec7f5d63ec85f4c80d1508bf99
[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
9 package org.opendaylight.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.netconf.api.NetconfMessage;
17 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
18 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
19 import org.opendaylight.netconf.client.NetconfClientSession;
20 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
21 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
22 import org.opendaylight.netconf.nettyutil.NeverReconnectStrategy;
23 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswordHandler;
24 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
25 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
26 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
27 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
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 final 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(), params.concurrentMessageLimit);
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() {
63         executionStrategy.invoke();
64         netconfClientSession.close();
65         return Boolean.TRUE;
66     }
67
68     private static ExecutionStrategy getExecutionStrategy(final Parameters params,
69             final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
70         if (params.async) {
71             return new AsyncExecutionStrategy(params, preparedMessages, sessionListener);
72         } else {
73             return new SyncExecutionStrategy(params, preparedMessages, sessionListener);
74         }
75     }
76
77     private static NetconfDeviceCommunicator getSessionListener(
78             final InetSocketAddress inetAddress, final int messageLimit) {
79         final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> loggingRemoteDevice =
80             new StressClient.LoggingRemoteDevice();
81         return new NetconfDeviceCommunicator(
82             new RemoteDeviceId("secure-test", inetAddress), loggingRemoteDevice, messageLimit);
83     }
84
85     private static NetconfClientConfiguration getNetconfClientConfiguration(final Parameters params,
86             final NetconfDeviceCommunicator sessionListener) {
87         final NetconfClientConfigurationBuilder netconfClientConfigurationBuilder = NetconfClientConfigurationBuilder
88             .create();
89         netconfClientConfigurationBuilder.withSessionListener(sessionListener);
90         netconfClientConfigurationBuilder.withAddress(params.getInetAddress());
91         if (params.tcpHeader != null) {
92             final String header = params.tcpHeader.replaceAll("\"", "").trim() + "\n";
93             netconfClientConfigurationBuilder.withAdditionalHeader(
94                 new NetconfHelloMessageAdditionalHeader(null, null, null, null, null) {
95                     @Override
96                     public String toFormattedString() {
97                         LOG.debug("Sending TCP header {}", header);
98                         return header;
99                     }
100                 });
101         }
102         netconfClientConfigurationBuilder.withProtocol(params.ssh ? NetconfClientConfiguration.NetconfClientProtocol.SSH
103             : NetconfClientConfiguration.NetconfClientProtocol.TCP);
104         netconfClientConfigurationBuilder.withAuthHandler(new LoginPasswordHandler(params.username, params.password));
105         netconfClientConfigurationBuilder.withConnectionTimeoutMillis(20000L);
106         netconfClientConfigurationBuilder.withReconnectStrategy(
107             new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
108         return netconfClientConfigurationBuilder.build();
109     }
110 }