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.controller.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.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
18 import org.opendaylight.controller.netconf.client.NetconfClientSession;
19 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
20 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
21 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
22 import org.opendaylight.controller.sal.connect.api.RemoteDevice;
23 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
24 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
25 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
26 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
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 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 this.sessionListener = getSessionListener(params.getInetAddress());
46 this.netconfClientDispatcher = netconfClientDispatcher;
47 cfg = getNetconfClientConfiguration(this.params, this.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 RuntimeException(e);
54 } catch (final ExecutionException e) {
55 throw new RuntimeException("Unable to connect", e);
57 executionStrategy = getExecutionStrategy(params, preparedMessages, sessionListener);
61 public Boolean call() throws Exception {
62 executionStrategy.invoke();
63 netconfClientSession.close();
67 private static ExecutionStrategy getExecutionStrategy(final Parameters params, final List<NetconfMessage> preparedMessages, final NetconfDeviceCommunicator sessionListener) {
69 return new AsyncExecutionStrategy(params, preparedMessages, sessionListener);
71 return new SyncExecutionStrategy(params, preparedMessages, sessionListener);
75 private static NetconfDeviceCommunicator getSessionListener(final InetSocketAddress inetAddress) {
76 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> loggingRemoteDevice = new StressClient.LoggingRemoteDevice();
77 return new NetconfDeviceCommunicator(new RemoteDeviceId("secure-test", inetAddress), loggingRemoteDevice);
80 private static NetconfClientConfiguration getNetconfClientConfiguration(final Parameters params, final NetconfDeviceCommunicator sessionListener) {
81 final NetconfClientConfigurationBuilder netconfClientConfigurationBuilder = NetconfClientConfigurationBuilder.create();
82 netconfClientConfigurationBuilder.withSessionListener(sessionListener);
83 netconfClientConfigurationBuilder.withAddress(params.getInetAddress());
84 if(params.tcpHeader != null) {
85 final String header = params.tcpHeader.replaceAll("\"", "").trim() + "\n";
86 netconfClientConfigurationBuilder.withAdditionalHeader(new NetconfHelloMessageAdditionalHeader(null, null, null, null, null) {
88 public String toFormattedString() {
89 LOG.debug("Sending TCP header {}", header);
94 netconfClientConfigurationBuilder.withProtocol(params.ssh ? NetconfClientConfiguration.NetconfClientProtocol.SSH : NetconfClientConfiguration.NetconfClientProtocol.TCP);
95 netconfClientConfigurationBuilder.withConnectionTimeoutMillis(20000L);
96 netconfClientConfigurationBuilder.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
97 return netconfClientConfigurationBuilder.build();