Remove unused exceptions
[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
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     private static final Logger LOG = LoggerFactory.getLogger(PerfClientCallable.class);
24
25     private final Parameters params;
26     private final ArrayList<Request> payloads;
27     private final AsyncHttpClient asyncHttpClient;
28     private ExecutionStrategy executionStrategy;
29
30     public PerfClientCallable(Parameters params, ArrayList<DestToPayload> payloads) {
31         this.params = params;
32         this.asyncHttpClient = new AsyncHttpClient(new AsyncHttpClientConfig.Builder()
33                 .setConnectTimeout(Integer.MAX_VALUE)
34                 .setRequestTimeout(Integer.MAX_VALUE)
35                 .setAllowPoolingConnections(true)
36                 .build());
37         this.payloads = new ArrayList<>();
38         for (DestToPayload payload : payloads) {
39             AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(payload.getDestination())
40                     .addHeader("content-type", "application/json")
41                     .addHeader("Accept", "application/xml")
42                     .setBody(payload.getPayload())
43                     .setRequestTimeout(Integer.MAX_VALUE);
44
45             if (params.auth != null) {
46                 requestBuilder.setRealm(new Realm.RealmBuilder()
47                         .setScheme(Realm.AuthScheme.BASIC)
48                         .setPrincipal(params.auth.get(0))
49                         .setPassword(params.auth.get(1))
50                         .setUsePreemptiveAuth(true)
51                         .build());
52             }
53
54             this.payloads.add(requestBuilder.build());
55         }
56         executionStrategy = getExecutionStrategy();
57     }
58
59     private ExecutionStrategy getExecutionStrategy() {
60         return params.async
61                 ? new AsyncExecutionStrategy(params, asyncHttpClient, payloads)
62                 : new SyncExecutionStrategy(params, asyncHttpClient, payloads);
63     }
64
65     @Override
66     public Void call() {
67         executionStrategy.invoke();
68         asyncHttpClient.closeAsynchronously();
69         return null;
70     }
71 }