Add 'features/protocol-framework/' from commit 'cb42405784db97d0ce2c5991d12a89b46d185949'
[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
9 package org.opendaylight.netconf.test.tool.client.stress;
10
11 import ch.qos.logback.classic.Level;
12 import com.google.common.base.Stopwatch;
13 import com.google.common.io.Files;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.util.HashedWheelTimer;
16 import io.netty.util.Timer;
17 import java.io.IOException;
18 import java.nio.charset.StandardCharsets;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.Future;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
27 import net.sourceforge.argparse4j.inf.ArgumentParser;
28 import net.sourceforge.argparse4j.inf.ArgumentParserException;
29 import org.opendaylight.controller.config.util.xml.XmlUtil;
30 import org.opendaylight.netconf.api.NetconfMessage;
31 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
32 import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
33 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
34 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
35 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
36 import org.opendaylight.netconf.test.tool.TestToolUtils;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.CommitInput;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditConfigInput;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.w3c.dom.Document;
43 import org.w3c.dom.Element;
44 import org.w3c.dom.Node;
45 import org.xml.sax.SAXException;
46
47 public final class StressClient {
48
49     private static final Logger LOG = LoggerFactory.getLogger(StressClient.class);
50
51     static final QName COMMIT_QNAME = QName.create(CommitInput.QNAME, "commit");
52     public static final NetconfMessage COMMIT_MSG;
53
54     static {
55         try {
56             COMMIT_MSG = new NetconfMessage(XmlUtil.readXmlToDocument(
57                 "<rpc message-id=\"commit-batch\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
58                     + "    <commit/>\n"
59                     + "</rpc>"));
60         } catch (final SAXException | IOException e) {
61             throw new ExceptionInInitializerError(e);
62         }
63     }
64
65     static final QName EDIT_QNAME = QName.create(EditConfigInput.QNAME, "edit-config");
66     static final org.w3c.dom.Document EDIT_CANDIDATE_BLUEPRINT;
67     static final org.w3c.dom.Document EDIT_RUNNING_BLUEPRINT;
68
69     static {
70         try {
71             EDIT_CANDIDATE_BLUEPRINT = XmlUtil.readXmlToDocument(
72                     "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
73                             + "    <edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
74                             + "        <target>\n"
75                             + "            <candidate/>\n"
76                             + "        </target>\n"
77                             + "        <default-operation>none</default-operation>"
78                             + "        <config/>\n"
79                             + "    </edit-config>\n"
80                             + "</rpc>");
81
82             EDIT_RUNNING_BLUEPRINT = XmlUtil.readXmlToDocument(
83                     "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
84                             + "    <edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
85                             + "        <target>\n"
86                             + "            <running/>\n"
87                             + "        </target>\n"
88                             + "        <default-operation>none</default-operation>"
89                             + "        <config/>\n"
90                             + "    </edit-config>\n"
91                             + "</rpc>");
92         } catch (SAXException | IOException e) {
93             throw new ExceptionInInitializerError(e);
94         }
95     }
96
97     private static final String MSG_ID_PLACEHOLDER_REGEX = "\\{MSG_ID\\}";
98     private static final String PHYS_ADDR_PLACEHOLDER = "{PHYS_ADDR}";
99
100     private static long macStart = 0xAABBCCDD0000L;
101
102     private static Parameters params;
103
104     public static void main(final String[] args) {
105
106         params = parseArgs(args, Parameters.getParser());
107         params.validate();
108
109         final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
110             .getLogger(Logger.ROOT_LOGGER_NAME);
111         root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
112
113         final int threadAmount = params.threadAmount;
114         LOG.info("thread amount: " + threadAmount);
115         final int requestsPerThread = params.editCount / params.threadAmount;
116         LOG.info("requestsPerThread: " + requestsPerThread);
117         final int leftoverRequests = params.editCount % params.threadAmount;
118         LOG.info("leftoverRequests: " + leftoverRequests);
119
120
121         LOG.info("Preparing messages");
122         // Prepare all msgs up front
123         final List<List<NetconfMessage>> allPreparedMessages = new ArrayList<>(threadAmount);
124         for (int i = 0; i < threadAmount; i++) {
125             if (i != threadAmount - 1) {
126                 allPreparedMessages.add(new ArrayList<NetconfMessage>(requestsPerThread));
127             } else {
128                 allPreparedMessages.add(new ArrayList<NetconfMessage>(requestsPerThread + leftoverRequests));
129             }
130         }
131
132
133         final String editContentString;
134         try {
135             editContentString = Files.toString(params.editContent, StandardCharsets.UTF_8);
136         } catch (final IOException e) {
137             throw new IllegalArgumentException("Cannot read content of " + params.editContent);
138         }
139
140         for (int i = 0; i < threadAmount; i++) {
141             final List<NetconfMessage> preparedMessages = allPreparedMessages.get(i);
142             int padding = 0;
143             if (i == threadAmount - 1) {
144                 padding = leftoverRequests;
145             }
146             for (int j = 0; j < requestsPerThread + padding; j++) {
147                 LOG.debug("id: " + (i * requestsPerThread + j));
148                 preparedMessages.add(prepareMessage(i * requestsPerThread + j, editContentString));
149             }
150         }
151
152         final NioEventLoopGroup nioGroup = new NioEventLoopGroup();
153         final Timer timer = new HashedWheelTimer();
154
155         final NetconfClientDispatcherImpl netconfClientDispatcher = configureClientDispatcher(params, nioGroup, timer);
156
157         final List<StressClientCallable> callables = new ArrayList<>(threadAmount);
158         for (final List<NetconfMessage> messages : allPreparedMessages) {
159             callables.add(new StressClientCallable(params, netconfClientDispatcher, messages));
160         }
161
162         final ExecutorService executorService = Executors.newFixedThreadPool(threadAmount);
163
164         LOG.info("Starting stress test");
165         final Stopwatch started = Stopwatch.createStarted();
166         try {
167             final List<Future<Boolean>> futures = executorService.invokeAll(callables);
168             for (final Future<Boolean> future : futures) {
169                 try {
170                     future.get(4L, TimeUnit.MINUTES);
171                 } catch (ExecutionException | TimeoutException e) {
172                     throw new RuntimeException(e);
173                 }
174             }
175             executorService.shutdownNow();
176         } catch (final InterruptedException e) {
177             throw new RuntimeException("Unable to execute requests", e);
178         }
179         started.stop();
180
181         LOG.info("FINISHED. Execution time: {}", started);
182         LOG.info("Requests per second: {}", (params.editCount * 1000.0 / started.elapsed(TimeUnit.MILLISECONDS)));
183
184         // Cleanup
185         timer.stop();
186         try {
187             nioGroup.shutdownGracefully().get(20L, TimeUnit.SECONDS);
188         } catch (InterruptedException | ExecutionException | TimeoutException e) {
189             LOG.warn("Unable to close executor properly", e);
190         }
191         //stop the underlying ssh thread that gets spawned if we use ssh
192         if (params.ssh) {
193             AsyncSshHandler.DEFAULT_CLIENT.stop();
194         }
195     }
196
197     static NetconfMessage prepareMessage(final int id, final String editContentString) {
198         final Document msg = XmlUtil.createDocumentCopy(
199             params.candidateDatastore ? EDIT_CANDIDATE_BLUEPRINT : EDIT_RUNNING_BLUEPRINT);
200         msg.getDocumentElement().setAttribute("message-id", Integer.toString(id));
201         final NetconfMessage netconfMessage = new NetconfMessage(msg);
202
203         final Element editContentElement;
204         try {
205             // Insert message id where needed
206             String specificEditContent = editContentString.replaceAll(MSG_ID_PLACEHOLDER_REGEX, Integer.toString(id));
207
208             final StringBuilder stringBuilder = new StringBuilder(specificEditContent);
209             int idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
210             while (idx != -1) {
211                 stringBuilder.replace(idx, idx + PHYS_ADDR_PLACEHOLDER.length(), TestToolUtils.getMac(macStart++));
212                 idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
213             }
214             specificEditContent = stringBuilder.toString();
215
216             editContentElement = XmlUtil.readXmlToElement(specificEditContent);
217             final Node config = ((Element) msg.getDocumentElement().getElementsByTagName("edit-config").item(0))
218                     .getElementsByTagName("config").item(0);
219             config.appendChild(msg.importNode(editContentElement, true));
220         } catch (final IOException | SAXException e) {
221             throw new IllegalArgumentException("Edit content file is unreadable", e);
222         }
223
224         return netconfMessage;
225     }
226
227     private static NetconfClientDispatcherImpl configureClientDispatcher(final Parameters params,
228             final NioEventLoopGroup nioGroup, final Timer timer) {
229         final NetconfClientDispatcherImpl netconfClientDispatcher;
230         if (params.exi) {
231             if (params.legacyFraming) {
232                 netconfClientDispatcher = ConfigurableClientDispatcher.createLegacyExi(nioGroup, nioGroup, timer);
233             } else {
234                 netconfClientDispatcher = ConfigurableClientDispatcher.createChunkedExi(nioGroup, nioGroup, timer);
235             }
236         } else {
237             if (params.legacyFraming) {
238                 netconfClientDispatcher = ConfigurableClientDispatcher.createLegacy(nioGroup, nioGroup, timer);
239             } else {
240                 netconfClientDispatcher = ConfigurableClientDispatcher.createChunked(nioGroup, nioGroup, timer);
241             }
242         }
243         return netconfClientDispatcher;
244     }
245
246     private static Parameters parseArgs(final String[] args, final ArgumentParser parser) {
247         final Parameters opt = new Parameters();
248         try {
249             parser.parseArgs(args, opt);
250             return opt;
251         } catch (final ArgumentParserException e) {
252             parser.handleError(e);
253         }
254
255         System.exit(1);
256         return null;
257     }
258
259     static class LoggingRemoteDevice
260             implements RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> {
261         @Override
262         public void onRemoteSessionUp(final NetconfSessionPreferences remoteSessionCapabilities,
263                                       final NetconfDeviceCommunicator netconfDeviceCommunicator) {
264             LOG.info("Session established");
265         }
266
267         @Override
268         public void onRemoteSessionDown() {
269             LOG.info("Session down");
270         }
271
272         @Override
273         public void onRemoteSessionFailed(final Throwable throwable) {
274             LOG.info("Session failed");
275         }
276
277         @Override
278         public void onNotification(final NetconfMessage notification) {
279             LOG.info("Notification received: {}", notification.toString());
280         }
281     }
282
283 }