Use Java 11 HttpClient
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / client / http / perf / PerfClientCallable.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 java.net.Authenticator;
11 import java.net.PasswordAuthentication;
12 import java.net.http.HttpClient;
13 import java.net.http.HttpClient.Builder;
14 import java.util.concurrent.Callable;
15 import org.opendaylight.netconf.test.tool.client.http.perf.RestPerfClient.RequestData;
16 import org.opendaylight.netconf.test.tool.client.stress.ExecutionStrategy;
17
18 public class PerfClientCallable implements Callable<Void> {
19     private final ExecutionStrategy executionStrategy;
20
21     public PerfClientCallable(final Parameters params, final RequestData payloads) {
22         final Builder builder = HttpClient.newBuilder();
23         if (params.auth != null) {
24             builder.authenticator(new Authenticator() {
25                 @Override
26                 protected PasswordAuthentication getPasswordAuthentication() {
27                     return new PasswordAuthentication(params.auth.get(0), params.auth.get(1).toCharArray());
28                 }
29             });
30         }
31
32         this.executionStrategy = params.async
33             ? new AsyncExecutionStrategy(params, builder.build(), payloads)
34             : new SyncExecutionStrategy(builder.build(), payloads);
35     }
36
37     @Override
38     public Void call() {
39         executionStrategy.invoke();
40         return null;
41     }
42 }