Refresh IETF client/server models
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / client / stress / StressClient.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.test.tool.client.stress;
9
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;
17 import java.util.Set;
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.rev240208.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.rev240208.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.rev240208.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.rev240208.ssh.client.grouping.ClientIdentityBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ssh.client.rev240208.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;
53
54 public final class StressClient {
55     private static final Logger LOG = LoggerFactory.getLogger(StressClient.class);
56
57     static final RemoteDevice<NetconfDeviceCommunicator> LOGGING_REMOTE_DEVICE = new RemoteDevice<>() {
58         @Override
59         public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
60                 final NetconfDeviceCommunicator netconfDeviceCommunicator) {
61             LOG.info("Session established");
62         }
63
64         @Override
65         public void onRemoteSessionDown() {
66             LOG.info("Session down");
67         }
68
69         @Override
70         public void onNotification(final NetconfMessage notification) {
71             LOG.info("Notification received: {}", notification);
72         }
73     };
74
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">
78             <commit/>
79         </rpc>"""));
80
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">
84             <edit-config>
85                 <target>
86                     <candidate/>
87                 </target>
88                 <default-operation>none</default-operation>
89                 <config/>
90             </edit-config>
91         </rpc>""");
92     static final Document EDIT_RUNNING_BLUEPRINT  = readString("""
93         <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
94             <edit-config>
95                 <target>
96                     <running/>
97                 </target>
98                 <default-operation>none</default-operation>
99                 <config/>
100             </edit-config>
101         </rpc>""");
102
103     private static Document readString(final String str) {
104         try {
105             return XmlUtil.readXmlToDocument(str);
106         } catch (SAXException | IOException e) {
107             throw new ExceptionInInitializerError(e);
108         }
109     }
110
111     private static final String MSG_ID_PLACEHOLDER_REGEX = "\\{MSG_ID\\}";
112     private static final String PHYS_ADDR_PLACEHOLDER = "{PHYS_ADDR}";
113
114     private static long macStart = 0xAABBCCDD0000L;
115
116     private static Parameters params;
117
118     private StressClient() {
119         // Hidden on purpose
120     }
121
122     public static void main(final String[] args) throws ExecutionException, InterruptedException, TimeoutException {
123         if (initParameters(args)) {
124             return;
125         }
126         params.validate();
127
128         final var root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
129         root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
130
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);
137
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));
144             } else {
145                 allPreparedMessages.add(new ArrayList<>(requestsPerThread + leftoverRequests));
146             }
147         }
148
149
150         final String editContentString;
151         try {
152             editContentString = Files.readString(params.editContent.toPath());
153         } catch (final IOException e) {
154             throw new IllegalArgumentException("Cannot read content of " + params.editContent, e);
155         }
156
157         for (int i = 0; i < threadAmount; i++) {
158             final var preparedMessages = allPreparedMessages.get(i);
159             int padding = 0;
160             if (i == threadAmount - 1) {
161                 padding = leftoverRequests;
162             }
163             for (int j = 0; j < requestsPerThread + padding; j++) {
164                 LOG.debug("id: {}", i * requestsPerThread + j);
165                 preparedMessages.add(prepareMessage(i * requestsPerThread + j, editContentString));
166             }
167         }
168
169         try (var timer = new DefaultNetconfTimer()) {
170             try (var netconfClientFactory = new NetconfClientFactoryImpl(timer)) {
171
172                 final var callables = new ArrayList<StressClientCallable>(threadAmount);
173                 for (var messages : allPreparedMessages) {
174                     callables.add(new StressClientCallable(params, netconfClientFactory, getBaseConfiguration(),
175                         messages));
176                 }
177
178                 final var executorService = Executors.newFixedThreadPool(threadAmount);
179
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);
185                 }
186                 executorService.shutdownNow();
187                 sw.stop();
188
189                 LOG.info("FINISHED. Execution time: {}", sw);
190                 LOG.info("Requests per second: {}", params.editCount * 1000.0 / sw.elapsed(TimeUnit.MILLISECONDS));
191             }
192         }
193     }
194
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);
200
201         final Element editContentElement;
202         try {
203             // Insert message id where needed
204             String specificEditContent = editContentString.replaceAll(MSG_ID_PLACEHOLDER_REGEX, Integer.toString(id));
205
206             final var sb = new StringBuilder(specificEditContent);
207             int idx = sb.indexOf(PHYS_ADDR_PLACEHOLDER);
208             while (idx != -1) {
209                 sb.replace(idx, idx + PHYS_ADDR_PLACEHOLDER.length(), TestToolUtils.getMac(macStart++));
210                 idx = sb.indexOf(PHYS_ADDR_PLACEHOLDER);
211             }
212             specificEditContent = sb.toString();
213
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);
220         }
221
222         return netconfMessage;
223     }
224
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();
229         try {
230             parser.parseArgs(args, args);
231         } catch (ArgumentParserException e) {
232             parser.handleError(e);
233             System.exit(1);
234             return true;
235         }
236         return false;
237     }
238
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());
248         if (params.ssh) {
249             confBuilder.withSshParameters(new SshClientParametersBuilder()
250                 .setClientIdentity(new ClientIdentityBuilder()
251                     .setUsername(params.username)
252                     .setPassword(new PasswordBuilder()
253                         .setPasswordType(
254                             new CleartextPasswordBuilder().setCleartextPassword(params.password).build())
255                         .build())
256                     .build())
257                 .build());
258         }
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) {
263                     @Override
264                     public String toFormattedString() {
265                         LOG.debug("Sending TCP header {}", header);
266                         return header;
267                     }
268                 });
269         }
270         return confBuilder.build();
271     }
272
273     private static Set<String> getCapabilities() {
274         if (params.exi) {
275             return params.legacyFraming
276                 // EXI + ]]gt;]]gt; framing.
277                 ? NetconfClientSessionNegotiatorFactory.LEGACY_EXI_CLIENT_CAPABILITIES
278                 // EXI + chunked framing
279                 : NetconfClientSessionNegotiatorFactory.EXI_CLIENT_CAPABILITIES;
280         }
281         return params.legacyFraming
282             // ]]gt;]]gt; framing.
283             ? NetconfClientSessionNegotiatorFactory.LEGACY_FRAMING_CLIENT_CAPABILITIES
284             // Chunked framing
285             : NetconfClientSessionNegotiatorFactory.DEFAULT_CLIENT_CAPABILITIES;
286     }
287 }