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 io.netty.util.concurrent.GlobalEventExecutor;
11 import java.net.InetSocketAddress;
12 import java.util.List;
13 import java.util.concurrent.Callable;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.netconf.api.NetconfMessage;
16 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
17 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
18 import org.opendaylight.netconf.client.NetconfClientSession;
19 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
20 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
21 import org.opendaylight.netconf.nettyutil.NeverReconnectStrategy;
22 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswordHandler;
23 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
24 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
25 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
26 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class StressClientCallable implements Callable<Boolean> {
32 private static final Logger LOG = LoggerFactory.getLogger(StressClientCallable.class);
34 private final Parameters params;
35 private final NetconfDeviceCommunicator sessionListener;
36 private final NetconfClientDispatcherImpl netconfClientDispatcher;
37 private final NetconfClientConfiguration cfg;
38 private final NetconfClientSession netconfClientSession;
39 private final ExecutionStrategy executionStrategy;
41 public StressClientCallable(final Parameters params,
42 final NetconfClientDispatcherImpl netconfClientDispatcher,
43 final List<NetconfMessage> preparedMessages) {
45 sessionListener = getSessionListener(params.getInetAddress(), params.concurrentMessageLimit);
46 this.netconfClientDispatcher = netconfClientDispatcher;
47 cfg = getNetconfClientConfiguration(this.params, sessionListener);
49 LOG.info("Connecting to netconf server {}:{}", params.ip, params.port);
51 netconfClientSession = netconfClientDispatcher.createClient(cfg).get();
52 } catch (final InterruptedException e) {
53 throw new IllegalStateException(e);
54 } catch (final ExecutionException e) {
55 throw new IllegalStateException("Unable to connect", e);
57 executionStrategy = getExecutionStrategy(params, preparedMessages, sessionListener);
61 public Boolean call() {
62 executionStrategy.invoke();
63 netconfClientSession.close();
67 private static ExecutionStrategy getExecutionStrategy(final Parameters params,
68 final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
70 return new AsyncExecutionStrategy(params, preparedMessages, sessionListener);
72 return new SyncExecutionStrategy(params, preparedMessages, sessionListener);
76 private static NetconfDeviceCommunicator getSessionListener(
77 final InetSocketAddress inetAddress, final int messageLimit) {
78 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> loggingRemoteDevice =
79 new StressClient.LoggingRemoteDevice();
80 return new NetconfDeviceCommunicator(
81 new RemoteDeviceId("secure-test", inetAddress), loggingRemoteDevice, messageLimit);
84 private static NetconfClientConfiguration getNetconfClientConfiguration(final Parameters params,
85 final NetconfDeviceCommunicator sessionListener) {
86 final NetconfClientConfigurationBuilder netconfClientConfigurationBuilder = NetconfClientConfigurationBuilder
88 netconfClientConfigurationBuilder.withSessionListener(sessionListener);
89 netconfClientConfigurationBuilder.withAddress(params.getInetAddress());
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) {
95 public String toFormattedString() {
96 LOG.debug("Sending TCP header {}", header);
101 netconfClientConfigurationBuilder.withProtocol(params.ssh ? NetconfClientConfiguration.NetconfClientProtocol.SSH
102 : NetconfClientConfiguration.NetconfClientProtocol.TCP);
103 netconfClientConfigurationBuilder.withAuthHandler(new LoginPasswordHandler(params.username, params.password));
104 netconfClientConfigurationBuilder.withConnectionTimeoutMillis(20000L);
105 netconfClientConfigurationBuilder.withReconnectStrategy(
106 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
107 return netconfClientConfigurationBuilder.build();