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