Bug 4725 - RestPerfClient: Add support for authentication. 80/31080/3
authorJakub Morvay <jmorvay@cisco.com>
Wed, 9 Dec 2015 15:47:37 +0000 (16:47 +0100)
committerJakub Morvay <jmorvay@cisco.com>
Thu, 10 Dec 2015 09:16:50 +0000 (10:16 +0100)
Add HTTP basic authentication support to RestPerfClient.

Change-Id: Idc9537b1e4018abe672bec38a3291a40251d53e8
Signed-off-by: Jakub Morvay <jmorvay@cisco.com>
opendaylight/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/client/http/perf/Parameters.java
opendaylight/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/client/http/perf/PerfClientCallable.java

index 32bb6a36b983fdb43830c0017cbf6d4a74355d85..8ef49f3511a555073f421304ffb40c0dc2d9d51f 100644 (file)
@@ -13,6 +13,7 @@ import java.io.File;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
+import java.util.ArrayList;
 import net.sourceforge.argparse4j.ArgumentParsers;
 import net.sourceforge.argparse4j.annotation.Arg;
 import net.sourceforge.argparse4j.inf.ArgumentParser;
@@ -49,6 +50,9 @@ public class Parameters {
     @Arg(dest = "throttle")
     public int throttle;
 
+    @Arg(dest = "auth")
+    public ArrayList<String> auth;
+
     static ArgumentParser getParser() {
         final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf stress client");
 
@@ -113,6 +117,11 @@ public class Parameters {
                         "with mutltiple threads this gets divided among all threads")
                 .dest("throttle");
 
+        parser.addArgument("--auth")
+                .nargs(2)
+                .help("Username and password for HTTP basic authentication in order username password.")
+                .dest("auth");
+
         return parser;
     }
 
index e073fbcb2624d9e1cf9edbb413afd8fd319874c8..fed504abb0d212c0cce3cb911518270e0f503d6a 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.netconf.test.tool.client.http.perf;
 
 import com.ning.http.client.AsyncHttpClient;
 import com.ning.http.client.AsyncHttpClientConfig;
+import com.ning.http.client.Realm;
 import com.ning.http.client.Request;
 import java.util.ArrayList;
 import java.util.concurrent.Callable;
@@ -36,12 +37,22 @@ public class PerfClientCallable implements Callable<Void>{
                 .build());
         this.payloads = new ArrayList<>();
         for (DestToPayload payload : payloads) {
-            this.payloads.add(asyncHttpClient.preparePost(payload.getDestination())
+            AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(payload.getDestination())
                     .addHeader("content-type", "application/json")
                     .addHeader("Accept", "application/xml")
                     .setBody(payload.getPayload())
-                    .setRequestTimeout(Integer.MAX_VALUE)
-                    .build());
+                    .setRequestTimeout(Integer.MAX_VALUE);
+
+            if(params.auth != null) {
+                requestBuilder.setRealm(new Realm.RealmBuilder()
+                        .setScheme(Realm.AuthScheme.BASIC)
+                        .setPrincipal(params.auth.get(0))
+                        .setPassword(params.auth.get(1))
+                        .setUsePreemptiveAuth(true)
+                        .build());
+            }
+
+            this.payloads.add(requestBuilder.build());
         }
         executionStrategy = getExecutionStrategy();
     }