Fix findbugs violations in netconf
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / Execution.java
index 5fc8d9ba9f18ee4c36335ae23950a9183b5f22a3..d4e6683278d10178e5613fe1a81edc273a6c0b1e 100644 (file)
@@ -9,15 +9,20 @@
 
 package org.opendaylight.netconf.test.tool;
 
-import com.ning.http.client.*;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
+import com.ning.http.client.AsyncCompletionHandler;
+import com.ning.http.client.AsyncHttpClient;
+import com.ning.http.client.AsyncHttpClientConfig;
+import com.ning.http.client.HttpResponseStatus;
+import com.ning.http.client.Realm;
+import com.ning.http.client.Request;
+import com.ning.http.client.Response;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Semaphore;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class Execution implements Callable<Void> {
 
@@ -33,7 +38,7 @@ public class Execution implements Callable<Void> {
         private final String destination;
         private final String payload;
 
-        public DestToPayload(String destination, String payload) {
+        DestToPayload(String destination, String payload) {
             this.destination = destination;
             this.payload = payload;
         }
@@ -64,9 +69,9 @@ public class Execution implements Callable<Void> {
 
         this.payloads = new ArrayList<>();
         for (DestToPayload payload : payloads) {
-            AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePut(payload.getDestination())
-                    .addHeader("Content-Type", "application/xml")
-                    .addHeader("Accept", "application/xml")
+            AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(payload.getDestination())
+                    .addHeader("Content-Type", "application/json")
+                    .addHeader("Accept", "application/json")
                     .setBody(payload.getPayload())
                     .setRequestTimeout(Integer.MAX_VALUE);
 
@@ -75,7 +80,7 @@ public class Execution implements Callable<Void> {
                         .setScheme(Realm.AuthScheme.BASIC)
                         .setPrincipal(params.auth.get(0))
                         .setPassword(params.auth.get(1))
-                        .setMethodName("PUT")
+                        .setMethodName("POST")
                         .setUsePreemptiveAuth(true)
                         .build());
             }
@@ -89,9 +94,14 @@ public class Execution implements Callable<Void> {
             try {
                 Response response = asyncHttpClient.executeRequest(request).get();
                 if (response.getStatusCode() != 200 && response.getStatusCode() != 204) {
-                    LOG.warn("Status code: {}", response.getStatusCode());
-                    LOG.warn("url: {}", request.getUrl());
-                    LOG.warn(response.getResponseBody());
+                    if (response.getStatusCode() == 409) {
+                        LOG.warn("Request failed, status code: {} - one or more of the devices"
+                                + " is already configured, skipping the whole batch", response.getStatusCode());
+                    } else {
+                        LOG.warn("Status code: {}", response.getStatusCode());
+                        LOG.warn("url: {}", request.getUrl());
+                        LOG.warn(response.getResponseBody());
+                    }
                 }
             } catch (InterruptedException | ExecutionException | IOException e) {
                 LOG.warn(e.toString());
@@ -101,7 +111,6 @@ public class Execution implements Callable<Void> {
     }
 
     private void invokeAsync() {
-        final ArrayList<ListenableFuture<Response>> futures = new ArrayList<>();
         LOG.info("Begin sending async requests");
 
         for (final Request request : payloads) {
@@ -110,13 +119,19 @@ public class Execution implements Callable<Void> {
             } catch (InterruptedException e) {
                 LOG.warn("Semaphore acquire interrupted");
             }
-            futures.add(asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Response>() {
+            asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Response>() {
                 @Override
                 public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
                     super.onStatusReceived(status);
                     if (status.getStatusCode() != 200 && status.getStatusCode() != 204) {
-                        LOG.warn("Request failed, status code: {}", status.getStatusCode() + status.getStatusText());
-                        LOG.warn("request: {}", request.toString());
+                        if (status.getStatusCode() == 409) {
+                            LOG.warn("Request failed, status code: {} - one or more of the devices"
+                                    + " is already configured, skipping the whole batch", status.getStatusCode());
+                        } else {
+                            LOG.warn("Request failed, status code: {}",
+                                status.getStatusCode() + status.getStatusText());
+                            LOG.warn("request: {}", request.toString());
+                        }
                     }
                     return STATE.CONTINUE;
                 }
@@ -126,7 +141,7 @@ public class Execution implements Callable<Void> {
                     semaphore.release();
                     return response;
                 }
-            }));
+            });
         }
         LOG.info("Requests sent, waiting for responses");