2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.test.tool.client.stress;
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;
26 public class StressClientCallable implements Callable<Boolean> {
28 private static final Logger LOG = LoggerFactory.getLogger(StressClientCallable.class);
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;
37 public StressClientCallable(final Parameters params,
38 final NetconfClientDispatcherImpl netconfClientDispatcher,
39 final List<NetconfMessage> preparedMessages) {
41 sessionListener = getSessionListener(params.getInetAddress(), params.concurrentMessageLimit);
42 this.netconfClientDispatcher = netconfClientDispatcher;
43 cfg = getNetconfClientConfiguration(this.params, sessionListener);
45 LOG.info("Connecting to netconf server {}:{}", params.ip, params.port);
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);
53 executionStrategy = getExecutionStrategy(params, preparedMessages, sessionListener);
57 public Boolean call() {
58 executionStrategy.invoke();
59 netconfClientSession.close();
63 private static ExecutionStrategy getExecutionStrategy(final Parameters params,
64 final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
66 return new AsyncExecutionStrategy(params, preparedMessages, sessionListener);
68 return new SyncExecutionStrategy(params, preparedMessages, sessionListener);
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);
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);
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) {
93 public String toFormattedString() {
94 LOG.debug("Sending TCP header {}", header);
99 return netconfClientConfigurationBuilder.build();