Make LoggingRemoteDevice a singleton
[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.messages.NetconfHelloMessageAdditionalHeader;
15 import org.opendaylight.netconf.api.messages.NetconfMessage;
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.RemoteDeviceId;
22 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswordHandler;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class StressClientCallable implements Callable<Boolean> {
27
28     private static final Logger LOG = LoggerFactory.getLogger(StressClientCallable.class);
29
30     private final Parameters params;
31     private final NetconfDeviceCommunicator sessionListener;
32     private final NetconfClientDispatcherImpl netconfClientDispatcher;
33     private final NetconfClientConfiguration cfg;
34     private final NetconfClientSession netconfClientSession;
35     private final ExecutionStrategy executionStrategy;
36
37     public StressClientCallable(final Parameters params,
38                                 final NetconfClientDispatcherImpl netconfClientDispatcher,
39                                 final List<NetconfMessage> preparedMessages) {
40         this.params = params;
41         sessionListener = getSessionListener(params.getInetAddress(), params.concurrentMessageLimit);
42         this.netconfClientDispatcher = netconfClientDispatcher;
43         cfg = getNetconfClientConfiguration(this.params, sessionListener);
44
45         LOG.info("Connecting to netconf server {}:{}", params.ip, params.port);
46         try {
47             netconfClientSession = netconfClientDispatcher.createClient(cfg).get();
48         } catch (final InterruptedException e) {
49             throw new IllegalStateException(e);
50         } catch (final ExecutionException e) {
51             throw new IllegalStateException("Unable to connect", e);
52         }
53         executionStrategy = getExecutionStrategy(params, preparedMessages, sessionListener);
54     }
55
56     @Override
57     public Boolean call() {
58         executionStrategy.invoke();
59         netconfClientSession.close();
60         return Boolean.TRUE;
61     }
62
63     private static ExecutionStrategy getExecutionStrategy(final Parameters params,
64             final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
65         if (params.async) {
66             return new AsyncExecutionStrategy(params, preparedMessages, sessionListener);
67         } else {
68             return new SyncExecutionStrategy(params, preparedMessages, sessionListener);
69         }
70     }
71
72     private static NetconfDeviceCommunicator getSessionListener(final InetSocketAddress inetAddress,
73             final int messageLimit) {
74         return new NetconfDeviceCommunicator(new RemoteDeviceId("secure-test", inetAddress),
75             StressClient.LOGGING_REMOTE_DEVICE, messageLimit);
76     }
77
78     private static NetconfClientConfiguration getNetconfClientConfiguration(final Parameters params,
79             final NetconfDeviceCommunicator sessionListener) {
80         final var netconfClientConfigurationBuilder = NetconfClientConfigurationBuilder.create()
81             .withSessionListener(sessionListener)
82             .withAddress(params.getInetAddress())
83             .withProtocol(params.ssh ? NetconfClientConfiguration.NetconfClientProtocol.SSH
84                 : NetconfClientConfiguration.NetconfClientProtocol.TCP)
85             .withAuthHandler(new LoginPasswordHandler(params.username, params.password))
86             .withConnectionTimeoutMillis(20000L);
87
88         if (params.tcpHeader != null) {
89             final String header = params.tcpHeader.replace("\"", "").trim() + "\n";
90             netconfClientConfigurationBuilder.withAdditionalHeader(
91                 new NetconfHelloMessageAdditionalHeader(null, null, null, null, null) {
92                     @Override
93                     public String toFormattedString() {
94                         LOG.debug("Sending TCP header {}", header);
95                         return header;
96                     }
97                 });
98         }
99         return netconfClientConfigurationBuilder.build();
100     }
101 }