Merge "NETCONF-557: Add support for URL capability"
[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 com.ning.http.client.AsyncHttpClient;
11 import com.ning.http.client.Request;
12 import com.ning.http.client.Response;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.concurrent.ExecutionException;
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
22     private static final Logger LOG = LoggerFactory.getLogger(SyncExecutionStrategy.class);
23
24     private final Parameters params;
25     private final ArrayList<Request> payloads;
26     private final AsyncHttpClient asyncHttpClient;
27
28     SyncExecutionStrategy(final Parameters params, final AsyncHttpClient asyncHttpClient,
29                           final ArrayList<Request> payloads) {
30         this.params = params;
31         this.asyncHttpClient = asyncHttpClient;
32         this.payloads = payloads;
33     }
34
35     @Override
36     public void invoke() {
37
38         LOG.info("Begin sending sync requests");
39         for (Request request : payloads) {
40             try {
41                 Response response = asyncHttpClient.executeRequest(request).get();
42                 if (response.getStatusCode() != 200 && response.getStatusCode() != 204) {
43                     LOG.warn("Status code: {}", response.getStatusCode());
44                     LOG.warn("url: {}", request.getUrl());
45                     LOG.warn("body: {}", response.getResponseBody());
46                 }
47             } catch (InterruptedException | ExecutionException | IOException e) {
48                 LOG.warn("Failed to execute request", e);
49             }
50         }
51         LOG.info("End sending sync requests");
52
53     }
54 }