Fix findbugs violations in netconf
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / Execution.java
1 /*
2  * Copyright (c) 2016 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
10 package org.opendaylight.netconf.test.tool;
11
12 import com.ning.http.client.AsyncCompletionHandler;
13 import com.ning.http.client.AsyncHttpClient;
14 import com.ning.http.client.AsyncHttpClientConfig;
15 import com.ning.http.client.HttpResponseStatus;
16 import com.ning.http.client.Realm;
17 import com.ning.http.client.Request;
18 import com.ning.http.client.Response;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Semaphore;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class Execution implements Callable<Void> {
28
29     private final ArrayList<Request> payloads;
30     private final AsyncHttpClient asyncHttpClient;
31     private static final Logger LOG = LoggerFactory.getLogger(Execution.class);
32     private final boolean invokeAsync;
33     private final Semaphore semaphore;
34     private final int throttle;
35
36     static final class DestToPayload {
37
38         private final String destination;
39         private final String payload;
40
41         DestToPayload(String destination, String payload) {
42             this.destination = destination;
43             this.payload = payload;
44         }
45
46         public String getDestination() {
47             return destination;
48         }
49
50         public String getPayload() {
51             return payload;
52         }
53     }
54
55     public Execution(TesttoolParameters params, ArrayList<DestToPayload> payloads) {
56         this.invokeAsync = params.async;
57         this.throttle = params.throttle / params.threadAmount;
58
59         if (params.async && params.threadAmount > 1) {
60             LOG.info("Throttling per thread: {}", this.throttle);
61         }
62         this.semaphore = new Semaphore(this.throttle);
63
64         this.asyncHttpClient = new AsyncHttpClient(new AsyncHttpClientConfig.Builder()
65                 .setConnectTimeout(Integer.MAX_VALUE)
66                 .setRequestTimeout(Integer.MAX_VALUE)
67                 .setAllowPoolingConnections(true)
68                 .build());
69
70         this.payloads = new ArrayList<>();
71         for (DestToPayload payload : payloads) {
72             AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(payload.getDestination())
73                     .addHeader("Content-Type", "application/json")
74                     .addHeader("Accept", "application/json")
75                     .setBody(payload.getPayload())
76                     .setRequestTimeout(Integer.MAX_VALUE);
77
78             if (params.auth != null) {
79                 requestBuilder.setRealm(new Realm.RealmBuilder()
80                         .setScheme(Realm.AuthScheme.BASIC)
81                         .setPrincipal(params.auth.get(0))
82                         .setPassword(params.auth.get(1))
83                         .setMethodName("POST")
84                         .setUsePreemptiveAuth(true)
85                         .build());
86             }
87             this.payloads.add(requestBuilder.build());
88         }
89     }
90
91     private void invokeSync() {
92         LOG.info("Begin sending sync requests");
93         for (Request request : payloads) {
94             try {
95                 Response response = asyncHttpClient.executeRequest(request).get();
96                 if (response.getStatusCode() != 200 && response.getStatusCode() != 204) {
97                     if (response.getStatusCode() == 409) {
98                         LOG.warn("Request failed, status code: {} - one or more of the devices"
99                                 + " is already configured, skipping the whole batch", response.getStatusCode());
100                     } else {
101                         LOG.warn("Status code: {}", response.getStatusCode());
102                         LOG.warn("url: {}", request.getUrl());
103                         LOG.warn(response.getResponseBody());
104                     }
105                 }
106             } catch (InterruptedException | ExecutionException | IOException e) {
107                 LOG.warn(e.toString());
108             }
109         }
110         LOG.info("End sending sync requests");
111     }
112
113     private void invokeAsync() {
114         LOG.info("Begin sending async requests");
115
116         for (final Request request : payloads) {
117             try {
118                 semaphore.acquire();
119             } catch (InterruptedException e) {
120                 LOG.warn("Semaphore acquire interrupted");
121             }
122             asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Response>() {
123                 @Override
124                 public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
125                     super.onStatusReceived(status);
126                     if (status.getStatusCode() != 200 && status.getStatusCode() != 204) {
127                         if (status.getStatusCode() == 409) {
128                             LOG.warn("Request failed, status code: {} - one or more of the devices"
129                                     + " is already configured, skipping the whole batch", status.getStatusCode());
130                         } else {
131                             LOG.warn("Request failed, status code: {}",
132                                 status.getStatusCode() + status.getStatusText());
133                             LOG.warn("request: {}", request.toString());
134                         }
135                     }
136                     return STATE.CONTINUE;
137                 }
138
139                 @Override
140                 public Response onCompleted(Response response) throws Exception {
141                     semaphore.release();
142                     return response;
143                 }
144             });
145         }
146         LOG.info("Requests sent, waiting for responses");
147
148         try {
149             semaphore.acquire(this.throttle);
150         } catch (InterruptedException e) {
151             LOG.warn("Semaphore acquire interrupted");
152         }
153
154         LOG.info("Responses received, ending...");
155     }
156
157     @Override
158     public Void call() throws Exception {
159         if (invokeAsync) {
160             this.invokeAsync();
161         } else {
162             this.invokeSync();
163         }
164         return null;
165     }
166 }