Bug 4725 - RestPerfClient: Add support for authentication.
[netconf.git] / opendaylight / 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
9 package org.opendaylight.netconf.test.tool.client.http.perf;
10
11 import com.ning.http.client.AsyncHttpClient;
12 import com.ning.http.client.AsyncHttpClientConfig;
13 import com.ning.http.client.Realm;
14 import com.ning.http.client.Request;
15 import java.util.ArrayList;
16 import java.util.concurrent.Callable;
17 import org.opendaylight.netconf.test.tool.client.http.perf.RestPerfClient.DestToPayload;
18 import org.opendaylight.netconf.test.tool.client.stress.ExecutionStrategy;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class PerfClientCallable implements Callable<Void>{
23
24     private static final Logger LOG = LoggerFactory.getLogger(PerfClientCallable.class);
25
26     private final Parameters params;
27     private final ArrayList<Request> payloads;
28     private final AsyncHttpClient asyncHttpClient;
29     private ExecutionStrategy executionStrategy;
30
31     public PerfClientCallable(Parameters params, ArrayList<DestToPayload> payloads) {
32         this.params = params;
33         this.asyncHttpClient = new AsyncHttpClient(new AsyncHttpClientConfig.Builder()
34                 .setConnectTimeout(Integer.MAX_VALUE)
35                 .setRequestTimeout(Integer.MAX_VALUE)
36                 .setAllowPoolingConnections(true)
37                 .build());
38         this.payloads = new ArrayList<>();
39         for (DestToPayload payload : payloads) {
40             AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(payload.getDestination())
41                     .addHeader("content-type", "application/json")
42                     .addHeader("Accept", "application/xml")
43                     .setBody(payload.getPayload())
44                     .setRequestTimeout(Integer.MAX_VALUE);
45
46             if(params.auth != null) {
47                 requestBuilder.setRealm(new Realm.RealmBuilder()
48                         .setScheme(Realm.AuthScheme.BASIC)
49                         .setPrincipal(params.auth.get(0))
50                         .setPassword(params.auth.get(1))
51                         .setUsePreemptiveAuth(true)
52                         .build());
53             }
54
55             this.payloads.add(requestBuilder.build());
56         }
57         executionStrategy = getExecutionStrategy();
58     }
59
60     private ExecutionStrategy getExecutionStrategy() {
61         return params.async
62                 ? new AsyncExecutionStrategy(params, asyncHttpClient, payloads)
63                 : new SyncExecutionStrategy(params, asyncHttpClient, payloads);
64     }
65
66     @Override
67     public Void call() throws Exception{
68
69         executionStrategy.invoke();
70         asyncHttpClient.closeAsynchronously();
71         return null;
72     }
73 }