Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / controller / 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.controller.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.Request;
14 import java.util.ArrayList;
15 import java.util.concurrent.Callable;
16 import org.opendaylight.controller.netconf.test.tool.client.http.perf.RestPerfClient.DestToPayload;
17 import org.opendaylight.controller.netconf.test.tool.client.stress.ExecutionStrategy;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class PerfClientCallable implements Callable<Void>{
22
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             this.payloads.add(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                     .build());
45         }
46         executionStrategy = getExecutionStrategy();
47     }
48
49     private ExecutionStrategy getExecutionStrategy() {
50         return params.async
51                 ? new AsyncExecutionStrategy(params, asyncHttpClient, payloads)
52                 : new SyncExecutionStrategy(params, asyncHttpClient, payloads);
53     }
54
55     @Override
56     public Void call() throws Exception{
57
58         executionStrategy.invoke();
59         asyncHttpClient.closeAsynchronously();
60         return null;
61     }
62 }