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 ch.qos.logback.classic.Level;
11 import com.google.common.base.Stopwatch;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.IOException;
14 import java.nio.file.Files;
15 import java.util.ArrayList;
16 import java.util.List;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import net.sourceforge.argparse4j.inf.ArgumentParserException;
23 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
24 import org.opendaylight.netconf.api.messages.NetconfMessage;
25 import org.opendaylight.netconf.api.xml.XmlUtil;
26 import org.opendaylight.netconf.client.NetconfClientFactoryImpl;
27 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
28 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
29 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
30 import org.opendaylight.netconf.client.mdsal.NetconfDeviceCommunicator;
31 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
32 import org.opendaylight.netconf.client.mdsal.api.RemoteDevice;
33 import org.opendaylight.netconf.common.impl.DefaultNetconfTimer;
34 import org.opendaylight.netconf.test.tool.TestToolUtils;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.CommitInput;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditConfigInput;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.crypto.types.rev231228.password.grouping.password.type.CleartextPasswordBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.client.rev231228.netconf.client.initiate.stack.grouping.transport.ssh.ssh.SshClientParametersBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.client.rev231228.netconf.client.initiate.stack.grouping.transport.ssh.ssh.TcpClientParametersBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.client.rev231228.ssh.client.grouping.ClientIdentityBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.client.rev231228.ssh.client.grouping.client.identity.PasswordBuilder;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.common.Uint16;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Element;
52 import org.xml.sax.SAXException;
54 public final class StressClient {
55 private static final Logger LOG = LoggerFactory.getLogger(StressClient.class);
57 static final RemoteDevice<NetconfDeviceCommunicator> LOGGING_REMOTE_DEVICE = new RemoteDevice<>() {
59 public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
60 final NetconfDeviceCommunicator netconfDeviceCommunicator) {
61 LOG.info("Session established");
65 public void onRemoteSessionDown() {
66 LOG.info("Session down");
70 public void onNotification(final NetconfMessage notification) {
71 LOG.info("Notification received: {}", notification);
75 static final QName COMMIT_QNAME = QName.create(CommitInput.QNAME, "commit");
76 public static final NetconfMessage COMMIT_MSG = new NetconfMessage(readString("""
77 <rpc message-id="commit-batch" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
81 static final QName EDIT_QNAME = QName.create(EditConfigInput.QNAME, "edit-config");
82 static final Document EDIT_CANDIDATE_BLUEPRINT = readString("""
83 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
88 <default-operation>none</default-operation>
92 static final Document EDIT_RUNNING_BLUEPRINT = readString("""
93 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
98 <default-operation>none</default-operation>
103 private static Document readString(final String str) {
105 return XmlUtil.readXmlToDocument(str);
106 } catch (SAXException | IOException e) {
107 throw new ExceptionInInitializerError(e);
111 private static final String MSG_ID_PLACEHOLDER_REGEX = "\\{MSG_ID\\}";
112 private static final String PHYS_ADDR_PLACEHOLDER = "{PHYS_ADDR}";
114 private static long macStart = 0xAABBCCDD0000L;
116 private static Parameters params;
118 private StressClient() {
122 public static void main(final String[] args) throws ExecutionException, InterruptedException, TimeoutException {
123 if (initParameters(args)) {
128 final var root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
129 root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
131 final int threadAmount = params.threadAmount;
132 LOG.info("thread amount: {}", threadAmount);
133 final int requestsPerThread = params.editCount / params.threadAmount;
134 LOG.info("requestsPerThread: {}", requestsPerThread);
135 final int leftoverRequests = params.editCount % params.threadAmount;
136 LOG.info("leftoverRequests: {}", leftoverRequests);
138 LOG.info("Preparing messages");
139 // Prepare all msgs up front
140 final var allPreparedMessages = new ArrayList<List<NetconfMessage>>(threadAmount);
141 for (int i = 0; i < threadAmount; i++) {
142 if (i != threadAmount - 1) {
143 allPreparedMessages.add(new ArrayList<>(requestsPerThread));
145 allPreparedMessages.add(new ArrayList<>(requestsPerThread + leftoverRequests));
150 final String editContentString;
152 editContentString = Files.readString(params.editContent.toPath());
153 } catch (final IOException e) {
154 throw new IllegalArgumentException("Cannot read content of " + params.editContent, e);
157 for (int i = 0; i < threadAmount; i++) {
158 final var preparedMessages = allPreparedMessages.get(i);
160 if (i == threadAmount - 1) {
161 padding = leftoverRequests;
163 for (int j = 0; j < requestsPerThread + padding; j++) {
164 LOG.debug("id: {}", i * requestsPerThread + j);
165 preparedMessages.add(prepareMessage(i * requestsPerThread + j, editContentString));
169 try (var timer = new DefaultNetconfTimer()) {
170 try (var netconfClientFactory = new NetconfClientFactoryImpl(timer)) {
172 final var callables = new ArrayList<StressClientCallable>(threadAmount);
173 for (var messages : allPreparedMessages) {
174 callables.add(new StressClientCallable(params, netconfClientFactory, getBaseConfiguration(),
178 final var executorService = Executors.newFixedThreadPool(threadAmount);
180 LOG.info("Starting stress test");
181 final var sw = Stopwatch.createStarted();
182 final var futures = executorService.invokeAll(callables);
183 for (var future : futures) {
184 future.get(4L, TimeUnit.MINUTES);
186 executorService.shutdownNow();
189 LOG.info("FINISHED. Execution time: {}", sw);
190 LOG.info("Requests per second: {}", params.editCount * 1000.0 / sw.elapsed(TimeUnit.MILLISECONDS));
195 static NetconfMessage prepareMessage(final int id, final String editContentString) {
196 final Document msg = XmlUtil.createDocumentCopy(
197 params.candidateDatastore ? EDIT_CANDIDATE_BLUEPRINT : EDIT_RUNNING_BLUEPRINT);
198 msg.getDocumentElement().setAttribute("message-id", Integer.toString(id));
199 final NetconfMessage netconfMessage = new NetconfMessage(msg);
201 final Element editContentElement;
203 // Insert message id where needed
204 String specificEditContent = editContentString.replaceAll(MSG_ID_PLACEHOLDER_REGEX, Integer.toString(id));
206 final var sb = new StringBuilder(specificEditContent);
207 int idx = sb.indexOf(PHYS_ADDR_PLACEHOLDER);
209 sb.replace(idx, idx + PHYS_ADDR_PLACEHOLDER.length(), TestToolUtils.getMac(macStart++));
210 idx = sb.indexOf(PHYS_ADDR_PLACEHOLDER);
212 specificEditContent = sb.toString();
214 editContentElement = XmlUtil.readXmlToElement(specificEditContent);
215 final var config = ((Element) msg.getDocumentElement().getElementsByTagName("edit-config").item(0))
216 .getElementsByTagName("config").item(0);
217 config.appendChild(msg.importNode(editContentElement, true));
218 } catch (final IOException | SAXException e) {
219 throw new IllegalArgumentException("Edit content file is unreadable", e);
222 return netconfMessage;
225 @SuppressFBWarnings(value = "DM_EXIT", justification = "Exit from CLI with error without throwing an exception")
226 private static boolean initParameters(final String[] args) {
227 final var parser = Parameters.getParser();
228 params = new Parameters();
230 parser.parseArgs(args, args);
231 } catch (ArgumentParserException e) {
232 parser.handleError(e);
239 private static NetconfClientConfiguration getBaseConfiguration() {
240 final var confBuilder = NetconfClientConfigurationBuilder.create()
241 .withProtocol(params.ssh ? NetconfClientConfiguration.NetconfClientProtocol.SSH
242 : NetconfClientConfiguration.NetconfClientProtocol.TCP)
243 .withConnectionTimeoutMillis(20000L)
244 .withOdlHelloCapabilities(getCapabilities().stream().map(Uri::new).toList())
245 .withTcpParameters(new TcpClientParametersBuilder()
246 .setRemoteAddress(new Host(IetfInetUtil.ipAddressFor(params.ip)))
247 .setRemotePort(new PortNumber(Uint16.valueOf(params.port))).build());
249 confBuilder.withSshParameters(new SshClientParametersBuilder()
250 .setClientIdentity(new ClientIdentityBuilder()
251 .setUsername(params.username)
252 .setPassword(new PasswordBuilder()
254 new CleartextPasswordBuilder().setCleartextPassword(params.password).build())
259 if (params.tcpHeader != null) {
260 final String header = params.tcpHeader.replace("\"", "").trim() + "\n";
261 confBuilder.withAdditionalHeader(
262 new NetconfHelloMessageAdditionalHeader(null, null, null, null, null) {
264 public String toFormattedString() {
265 LOG.debug("Sending TCP header {}", header);
270 return confBuilder.build();
273 private static Set<String> getCapabilities() {
275 return params.legacyFraming
276 // EXI + ]]gt;]]gt; framing.
277 ? NetconfClientSessionNegotiatorFactory.LEGACY_EXI_CLIENT_CAPABILITIES
278 // EXI + chunked framing
279 : NetconfClientSessionNegotiatorFactory.EXI_CLIENT_CAPABILITIES;
281 return params.legacyFraming
282 // ]]gt;]]gt; framing.
283 ? NetconfClientSessionNegotiatorFactory.LEGACY_FRAMING_CLIENT_CAPABILITIES
285 : NetconfClientSessionNegotiatorFactory.DEFAULT_CLIENT_CAPABILITIES;