Search in sources :

Example 26 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class StressClient method main.

public static void main(final String[] args) {
    params = parseArgs(args, Parameters.getParser());
    params.validate();
    final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
    final int threadAmount = params.threadAmount;
    LOG.info("thread amount: {}", threadAmount);
    final int requestsPerThread = params.editCount / params.threadAmount;
    LOG.info("requestsPerThread: {}", requestsPerThread);
    final int leftoverRequests = params.editCount % params.threadAmount;
    LOG.info("leftoverRequests: {}", leftoverRequests);
    LOG.info("Preparing messages");
    // Prepare all msgs up front
    final List<List<NetconfMessage>> allPreparedMessages = new ArrayList<>(threadAmount);
    for (int i = 0; i < threadAmount; i++) {
        if (i != threadAmount - 1) {
            allPreparedMessages.add(new ArrayList<>(requestsPerThread));
        } else {
            allPreparedMessages.add(new ArrayList<>(requestsPerThread + leftoverRequests));
        }
    }
    final String editContentString;
    try {
        editContentString = Files.asCharSource(params.editContent, StandardCharsets.UTF_8).read();
    } catch (final IOException e) {
        throw new IllegalArgumentException("Cannot read content of " + params.editContent, e);
    }
    for (int i = 0; i < threadAmount; i++) {
        final List<NetconfMessage> preparedMessages = allPreparedMessages.get(i);
        int padding = 0;
        if (i == threadAmount - 1) {
            padding = leftoverRequests;
        }
        for (int j = 0; j < requestsPerThread + padding; j++) {
            LOG.debug("id: {}", i * requestsPerThread + j);
            preparedMessages.add(prepareMessage(i * requestsPerThread + j, editContentString));
        }
    }
    final NioEventLoopGroup nioGroup = new NioEventLoopGroup();
    final Timer timer = new HashedWheelTimer();
    final NetconfClientDispatcherImpl netconfClientDispatcher = configureClientDispatcher(nioGroup, timer);
    final List<StressClientCallable> callables = new ArrayList<>(threadAmount);
    for (final List<NetconfMessage> messages : allPreparedMessages) {
        callables.add(new StressClientCallable(params, netconfClientDispatcher, messages));
    }
    final ExecutorService executorService = Executors.newFixedThreadPool(threadAmount);
    LOG.info("Starting stress test");
    final Stopwatch started = Stopwatch.createStarted();
    try {
        final List<Future<Boolean>> futures = executorService.invokeAll(callables);
        for (final Future<Boolean> future : futures) {
            try {
                future.get(4L, TimeUnit.MINUTES);
            } catch (ExecutionException | TimeoutException e) {
                throw new RuntimeException(e);
            }
        }
        executorService.shutdownNow();
    } catch (final InterruptedException e) {
        throw new RuntimeException("Unable to execute requests", e);
    }
    started.stop();
    LOG.info("FINISHED. Execution time: {}", started);
    LOG.info("Requests per second: {}", params.editCount * 1000.0 / started.elapsed(TimeUnit.MILLISECONDS));
    // Cleanup
    timer.stop();
    try {
        nioGroup.shutdownGracefully().get(20L, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOG.warn("Unable to close executor properly", e);
    }
    // stop the underlying ssh thread that gets spawned if we use ssh
    if (params.ssh) {
        AsyncSshHandler.DEFAULT_CLIENT.stop();
    }
}
Also used : ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) Logger(org.slf4j.Logger) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) TimeoutException(java.util.concurrent.TimeoutException) Stopwatch(com.google.common.base.Stopwatch) HashedWheelTimer(io.netty.util.HashedWheelTimer) IOException(java.io.IOException) NetconfClientDispatcherImpl(org.opendaylight.netconf.client.NetconfClientDispatcherImpl) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 27 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class StressClient method prepareMessage.

static NetconfMessage prepareMessage(final int id, final String editContentString) {
    final Document msg = XmlUtil.createDocumentCopy(params.candidateDatastore ? EDIT_CANDIDATE_BLUEPRINT : EDIT_RUNNING_BLUEPRINT);
    msg.getDocumentElement().setAttribute("message-id", Integer.toString(id));
    final NetconfMessage netconfMessage = new NetconfMessage(msg);
    final Element editContentElement;
    try {
        // Insert message id where needed
        String specificEditContent = editContentString.replaceAll(MSG_ID_PLACEHOLDER_REGEX, Integer.toString(id));
        final StringBuilder stringBuilder = new StringBuilder(specificEditContent);
        int idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
        while (idx != -1) {
            stringBuilder.replace(idx, idx + PHYS_ADDR_PLACEHOLDER.length(), TestToolUtils.getMac(macStart++));
            idx = stringBuilder.indexOf(PHYS_ADDR_PLACEHOLDER);
        }
        specificEditContent = stringBuilder.toString();
        editContentElement = XmlUtil.readXmlToElement(specificEditContent);
        final Node config = ((Element) msg.getDocumentElement().getElementsByTagName("edit-config").item(0)).getElementsByTagName("config").item(0);
        config.appendChild(msg.importNode(editContentElement, true));
    } catch (final IOException | SAXException e) {
        throw new IllegalArgumentException("Edit content file is unreadable", e);
    }
    return netconfMessage;
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 28 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class TestToolTest method invokeRpc.

private Document invokeRpc(final Configuration simulatorConfig, final String xmlRequest) throws Exception {
    // GIVEN
    int localPort = launchSimulator(simulatorConfig);
    SimpleNetconfClientSessionListener sessionListener = new SimpleNetconfClientSessionListener();
    NetconfClientConfiguration clientConfig = getClientConfig("localhost", localPort, simulatorConfig, sessionListener);
    Document docRequest = XmlUtil.readXmlToDocument(xmlRequest);
    NetconfMessage request = new NetconfMessage(docRequest);
    // WHEN
    NetconfMessage response;
    try (NetconfClientSession ignored = dispatcher.createClient(clientConfig).get()) {
        response = sessionListener.sendRequest(request).get(RECEIVE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    }
    // THEN
    assertNotNull(response);
    return response.getDocument();
}
Also used : NetconfClientConfiguration(org.opendaylight.netconf.client.conf.NetconfClientConfiguration) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) SimpleNetconfClientSessionListener(org.opendaylight.netconf.client.SimpleNetconfClientSessionListener) Document(org.w3c.dom.Document) NetconfClientSession(org.opendaylight.netconf.client.NetconfClientSession)

Example 29 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class SimulatedCreateSubscription method parseNetconfNotification.

private static NetconfMessage parseNetconfNotification(String content) {
    final int startEventTime = content.indexOf("<eventTime>") + "<eventTime>".length();
    final int endEventTime = content.indexOf("</eventTime>");
    final String eventTime = content.substring(startEventTime, endEventTime);
    if (eventTime.equals("XXXX")) {
        content = content.replace(eventTime, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(new Date()));
    }
    try {
        return new NetconfMessage(XmlUtil.readXmlToDocument(content));
    } catch (SAXException | IOException e) {
        throw new IllegalArgumentException("Cannot parse notifications", e);
    }
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) SAXException(org.xml.sax.SAXException)

Example 30 with NetconfMessage

use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.

the class NetconfEXIHandlersTest method setUp.

@Before
public void setUp() throws Exception {
    final NetconfEXICodec codec = NetconfEXICodec.forParameters(EXIParameters.empty());
    netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec);
    netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec);
    msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
    this.msgAsExi = msgToExi(msg, codec);
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Before(org.junit.Before)

Aggregations

NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)125 Test (org.junit.Test)72 AbstractBaseSchemasTest (org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest)40 Document (org.w3c.dom.Document)28 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)23 QName (org.opendaylight.yangtools.yang.common.QName)17 DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)15 Test (org.junit.jupiter.api.Test)13 SimpleNetconfClientSessionListener (org.opendaylight.netconf.client.SimpleNetconfClientSessionListener)13 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)13 Node (org.w3c.dom.Node)13 NetconfClientSession (org.opendaylight.netconf.client.NetconfClientSession)12 ArrayList (java.util.ArrayList)11 Element (org.w3c.dom.Element)11 DOMRpcResult (org.opendaylight.mdsal.dom.api.DOMRpcResult)10 UnkeyedListNode (org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode)10 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)9 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)9 ChannelFuture (io.netty.channel.ChannelFuture)8 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)8