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