Use Java 11 HttpClient
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / client / http / perf / SyncExecutionStrategy.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.http.perf;
9
10 import static org.opendaylight.netconf.test.tool.client.http.perf.RequestMessageUtils.formRequest;
11
12 import java.io.IOException;
13 import java.net.http.HttpClient;
14 import java.net.http.HttpResponse;
15 import java.net.http.HttpResponse.BodyHandlers;
16 import org.opendaylight.netconf.test.tool.client.stress.ExecutionStrategy;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class SyncExecutionStrategy implements ExecutionStrategy {
21     private static final Logger LOG = LoggerFactory.getLogger(SyncExecutionStrategy.class);
22
23     private final HttpClient httpClient;
24     private final RestPerfClient.RequestData payloads;
25
26     SyncExecutionStrategy(final HttpClient httpClient, final RestPerfClient.RequestData payloads) {
27         this.httpClient = httpClient;
28         this.payloads = payloads;
29     }
30
31     @Override
32     public void invoke() {
33
34         LOG.info("Begin sending sync requests");
35         for (int i = 0; i < payloads.getRequests(); i++) {
36             final String message = RequestMessageUtils.prepareMessage(payloads.getThreadId(), i,
37                     payloads.getContentString(), payloads.getPort());
38             final HttpResponse<String> response;
39             try {
40                 response = httpClient.send(formRequest(payloads.getDestination(), message), BodyHandlers.ofString());
41             } catch (InterruptedException | IOException e) {
42                 LOG.warn("Failed to execute request", e);
43                 return;
44             }
45
46             if (response.statusCode() != 200 && response.statusCode() != 204) {
47                 LOG.warn("Status code: {}", response.statusCode());
48                 LOG.warn("url: {}", response.uri());
49                 LOG.warn("body: {}", response.body());
50             }
51         }
52         LOG.info("End sending sync requests");
53     }
54 }