Rework NETCONF client reconnection
[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 java.net.InetSocketAddress;
11 import java.util.List;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.netconf.api.NetconfMessage;
15 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
16 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
17 import org.opendaylight.netconf.client.NetconfClientSession;
18 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
19 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
20 import org.opendaylight.netconf.client.mdsal.NetconfDeviceCommunicator;
21 import org.opendaylight.netconf.client.mdsal.api.RemoteDevice;
22 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
23 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswordHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class StressClientCallable implements Callable<Boolean> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(StressClientCallable.class);
30
31     private final Parameters params;
32     private final NetconfDeviceCommunicator sessionListener;
33     private final NetconfClientDispatcherImpl netconfClientDispatcher;
34     private final NetconfClientConfiguration cfg;
35     private final NetconfClientSession netconfClientSession;
36     private final ExecutionStrategy executionStrategy;
37
38     public StressClientCallable(final Parameters params,
39                                 final NetconfClientDispatcherImpl netconfClientDispatcher,
40                                 final List<NetconfMessage> preparedMessages) {
41         this.params = params;
42         sessionListener = getSessionListener(params.getInetAddress(), params.concurrentMessageLimit);
43         this.netconfClientDispatcher = netconfClientDispatcher;
44         cfg = getNetconfClientConfiguration(this.params, sessionListener);
45
46         LOG.info("Connecting to netconf server {}:{}", params.ip, params.port);
47         try {
48             netconfClientSession = netconfClientDispatcher.createClient(cfg).get();
49         } catch (final InterruptedException e) {
50             throw new IllegalStateException(e);
51         } catch (final ExecutionException e) {
52             throw new IllegalStateException("Unable to connect", e);
53         }
54         executionStrategy = getExecutionStrategy(params, preparedMessages, sessionListener);
55     }
56
57     @Override
58     public Boolean call() {
59         executionStrategy.invoke();
60         netconfClientSession.close();
61         return Boolean.TRUE;
62     }
63
64     private static ExecutionStrategy getExecutionStrategy(final Parameters params,
65             final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
66         if (params.async) {
67             return new AsyncExecutionStrategy(params, preparedMessages, sessionListener);
68         } else {
69             return new SyncExecutionStrategy(params, preparedMessages, sessionListener);
70         }
71     }
72
73     private static NetconfDeviceCommunicator getSessionListener(
74             final InetSocketAddress inetAddress, final int messageLimit) {
75         final RemoteDevice<NetconfDeviceCommunicator> loggingRemoteDevice = new StressClient.LoggingRemoteDevice();
76         return new NetconfDeviceCommunicator(
77             new RemoteDeviceId("secure-test", inetAddress), loggingRemoteDevice, messageLimit);
78     }
79
80     private static NetconfClientConfiguration getNetconfClientConfiguration(final Parameters params,
81             final NetconfDeviceCommunicator sessionListener) {
82         final var netconfClientConfigurationBuilder = NetconfClientConfigurationBuilder.create()
83             .withSessionListener(sessionListener)
84             .withAddress(params.getInetAddress())
85             .withProtocol(params.ssh ? NetconfClientConfiguration.NetconfClientProtocol.SSH
86                 : NetconfClientConfiguration.NetconfClientProtocol.TCP)
87             .withAuthHandler(new LoginPasswordHandler(params.username, params.password))
88             .withConnectionTimeoutMillis(20000L);
89
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         return netconfClientConfigurationBuilder.build();
102     }
103 }