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
9 package org.opendaylight.netconf.test.tool.client.stress;
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;
31 public class StressClientCallable implements Callable<Boolean> {
33 private static final Logger LOG = LoggerFactory.getLogger(StressClientCallable.class);
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;
42 public StressClientCallable(final Parameters params,
43 final NetconfClientDispatcherImpl netconfClientDispatcher,
44 final List<NetconfMessage> preparedMessages) {
46 this.sessionListener = getSessionListener(params.getInetAddress(), params.concurrentMessageLimit);
47 this.netconfClientDispatcher = netconfClientDispatcher;
48 cfg = getNetconfClientConfiguration(this.params, this.sessionListener);
50 LOG.info("Connecting to netconf server {}:{}", params.ip, params.port);
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);
58 executionStrategy = getExecutionStrategy(params, preparedMessages, sessionListener);
62 public Boolean call() {
63 executionStrategy.invoke();
64 netconfClientSession.close();
68 private static ExecutionStrategy getExecutionStrategy(final Parameters params,
69 final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
71 return new AsyncExecutionStrategy(params, preparedMessages, sessionListener);
73 return new SyncExecutionStrategy(params, preparedMessages, sessionListener);
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);
85 private static NetconfClientConfiguration getNetconfClientConfiguration(final Parameters params,
86 final NetconfDeviceCommunicator sessionListener) {
87 final NetconfClientConfigurationBuilder netconfClientConfigurationBuilder = NetconfClientConfigurationBuilder
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) {
96 public String toFormattedString() {
97 LOG.debug("Sending TCP header {}", header);
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();