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.handler.ssh.authentication.LoginPassword;
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.opendaylight.protocol.framework.NeverReconnectStrategy;
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 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());
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() throws Exception {
63 executionStrategy.invoke();
64 netconfClientSession.close();
68 private static ExecutionStrategy getExecutionStrategy(final Parameters params, 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(final InetSocketAddress inetAddress) {
77 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> loggingRemoteDevice = new StressClient.LoggingRemoteDevice();
78 return new NetconfDeviceCommunicator(new RemoteDeviceId("secure-test", inetAddress), loggingRemoteDevice);
81 private static NetconfClientConfiguration getNetconfClientConfiguration(final Parameters params, final NetconfDeviceCommunicator sessionListener) {
82 final NetconfClientConfigurationBuilder netconfClientConfigurationBuilder = NetconfClientConfigurationBuilder.create();
83 netconfClientConfigurationBuilder.withSessionListener(sessionListener);
84 netconfClientConfigurationBuilder.withAddress(params.getInetAddress());
85 if(params.tcpHeader != null) {
86 final String header = params.tcpHeader.replaceAll("\"", "").trim() + "\n";
87 netconfClientConfigurationBuilder.withAdditionalHeader(new NetconfHelloMessageAdditionalHeader(null, null, null, null, null) {
89 public String toFormattedString() {
90 LOG.debug("Sending TCP header {}", header);
95 netconfClientConfigurationBuilder.withProtocol(params.ssh ? NetconfClientConfiguration.NetconfClientProtocol.SSH : NetconfClientConfiguration.NetconfClientProtocol.TCP);
96 netconfClientConfigurationBuilder.withAuthHandler(new LoginPassword(params.username, params.password));
97 netconfClientConfigurationBuilder.withConnectionTimeoutMillis(20000L);
98 netconfClientConfigurationBuilder.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
99 return netconfClientConfigurationBuilder.build();