cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [44/45] git commit: updated refs/heads/master to 44ba14d
Date Tue, 25 Aug 2015 18:50:08 GMT
Use NSX specific RestClient in API implementation (NiciraNvpApi)

- Simplify public API to return Lists instead of NiciraNvpLists


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/09f71533
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/09f71533
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/09f71533

Branch: refs/heads/master
Commit: 09f715337882c432e8731f81bce6490362e1cc2d
Parents: c6602ee
Author: Miguel Ferreira <miguelferreira@me.com>
Authored: Sat Aug 22 17:30:58 2015 +0200
Committer: Miguel Ferreira <miguelferreira@me.com>
Committed: Tue Aug 25 17:36:17 2015 +0200

----------------------------------------------------------------------
 .../cloud/network/nicira/NiciraConstants.java   |  42 ++
 .../com/cloud/network/nicira/NiciraNvpApi.java  | 323 ++++++++-------
 .../network/resource/NiciraNvpResource.java     |  26 +-
 ...figurePortForwardingRulesCommandWrapper.java |   7 +-
 ...ciraNvpConfigurePublicIpsCommandWrapper.java |   9 +-
 ...vpConfigureStaticNatRulesCommandWrapper.java |   7 +-
 ...aNvpFindLogicalSwitchPortCommandWrapper.java |   7 +-
 .../cloud/network/nicira/NiciraNvpApiIT.java    | 202 ++++-----
 .../cloud/network/nicira/NiciraNvpApiTest.java  | 411 ++++++-------------
 .../resource/NiciraNvpRequestWrapperTest.java   |   7 +-
 .../network/resource/NiciraNvpResourceTest.java |  72 +---
 .../utils/rest/HttpUriRequestPathMatcher.java   |  43 ++
 12 files changed, 539 insertions(+), 617 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraConstants.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraConstants.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraConstants.java
new file mode 100644
index 0000000..31adf9d
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraConstants.java
@@ -0,0 +1,42 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.nicira;
+
+public class NiciraConstants {
+
+    public static final String SEC_PROFILE_URI_PREFIX = "/ws.v1/security-profile";
+    public static final String ACL_URI_PREFIX = "/ws.v1/acl";
+    public static final String SWITCH_URI_PREFIX = "/ws.v1/lswitch";
+    public static final String ROUTER_URI_PREFIX = "/ws.v1/lrouter";
+    public static final String LOGIN_URL = "/ws.v1/login";
+    public static final String CONTROL_CLUSTER_STATUS_URL = "/ws.v1/control-cluster/status";
+
+    public static final String ATTACHMENT_PATH_SEGMENT = "/attachment";
+    public static final String NAT_PATH_SEGMENT = "/nat";
+    public static final String LPORT_PATH_SEGMENT = "/lport";
+
+    public static final String ATTACHMENT_VIF_UUID_QUERY_PARAMETER_NAME = "attachment_vif_uuid";
+    public static final String ATTACHMENT_VLAN_PARAMETER = "attachment_vlan";
+    public static final String ATTACHMENT_GWSVC_UUID_QUERY_PARAMETER = "attachment_gwsvc_uuid";
+    public static final String WILDCARD_QUERY_PARAMETER = "*";
+    public static final String UUID_QUERY_PARAMETER = "uuid";
+    public static final String FIELDS_QUERY_PARAMETER = "fields";
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraNvpApi.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraNvpApi.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraNvpApi.java
index 39262b6..093d90d 100644
--- a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraNvpApi.java
+++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraNvpApi.java
@@ -20,32 +20,39 @@
 package com.cloud.network.nicira;
 
 import java.lang.reflect.Type;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.impl.client.CloseableHttpClient;
+
 import com.cloud.utils.rest.CloudstackRESTException;
 import com.cloud.utils.rest.RESTServiceConnector;
-import com.cloud.utils.rest.RESTValidationStrategy;
-import com.google.gson.JsonDeserializationContext;
+import com.google.common.base.Optional;
 import com.google.gson.JsonDeserializer;
 import com.google.gson.reflect.TypeToken;
 
 @SuppressWarnings("rawtypes")
 public class NiciraNvpApi {
-    protected static final String GET_METHOD_TYPE = "get";
-    protected static final String DELETE_METHOD_TYPE = "delete";
-    protected static final String PUT_METHOD_TYPE = "put";
-    protected static final String POST_METHOD_TYPE = "post";
 
-    protected static final String SEC_PROFILE_URI_PREFIX = "/ws.v1/security-profile";
-    protected static final String ACL_URI_PREFIX = "/ws.v1/acl";
-    private static final String SWITCH_URI_PREFIX = "/ws.v1/lswitch";
-    private static final String ROUTER_URI_PREFIX = "/ws.v1/lrouter";
-    private static final String LOGIN_URL = "/ws.v1/login";
+    private static final Optional<String> ABSENT = Optional.absent();
+
+    private static final String SWITCH_URI_PREFIX = NiciraConstants.SWITCH_URI_PREFIX;
+    private static final String ROUTER_URI_PREFIX = NiciraConstants.ROUTER_URI_PREFIX;
+
+    private static final String ATTACHMENT_PATH_SEGMENT = NiciraConstants.ATTACHMENT_PATH_SEGMENT;
+    private static final String NAT_PATH_SEGMENT = NiciraConstants.NAT_PATH_SEGMENT;
+    private static final String LPORT_PATH_SEGMENT = NiciraConstants.LPORT_PATH_SEGMENT;
+
+    private static final String ATTACHMENT_GWSVC_UUID_QUERY_PARAMETER = NiciraConstants.ATTACHMENT_GWSVC_UUID_QUERY_PARAMETER;
+    private static final String WILDCARD_QUERY_PARAMETER = NiciraConstants.WILDCARD_QUERY_PARAMETER;
+    private static final String UUID_QUERY_PARAMETER = NiciraConstants.UUID_QUERY_PARAMETER;
+    private static final String FIELDS_QUERY_PARAMETER = NiciraConstants.FIELDS_QUERY_PARAMETER;
+
+    private static final int DEFAULT_MAX_RETRIES = 5;
 
     private final RESTServiceConnector restConnector;
 
@@ -57,8 +64,8 @@ public class NiciraNvpApi {
 
     static {
         prefixMap = new HashMap<Class, String>();
-        prefixMap.put(SecurityProfile.class, SEC_PROFILE_URI_PREFIX);
-        prefixMap.put(Acl.class, ACL_URI_PREFIX);
+        prefixMap.put(SecurityProfile.class, NiciraConstants.SEC_PROFILE_URI_PREFIX);
+        prefixMap.put(Acl.class, NiciraConstants.ACL_URI_PREFIX);
         prefixMap.put(LogicalSwitch.class, SWITCH_URI_PREFIX);
         prefixMap.put(LogicalRouter.class, ROUTER_URI_PREFIX);
 
@@ -73,32 +80,31 @@ public class NiciraNvpApi {
         }.getType());
 
         defaultListParams = new HashMap<String, String>();
-        defaultListParams.put("fields", "*");
-    }
-
-    public NiciraNvpApi() {
-        final List<Class<?>> classList = new ArrayList<Class<?>>();
-        classList.add(NatRule.class);
-        classList.add(RoutingConfig.class);
-        final List<JsonDeserializer<?>> deserializerList = new ArrayList<JsonDeserializer<?>>();
-        deserializerList.add(new NatRuleAdapter());
-        deserializerList.add(new RoutingConfigAdapter());
-
-        restConnector = new RESTServiceConnector(new RESTValidationStrategy(LOGIN_URL), classList, deserializerList);
+        defaultListParams.put(FIELDS_QUERY_PARAMETER, WILDCARD_QUERY_PARAMETER);
     }
 
-    public NiciraNvpApi(final String address, final String username, final String password) {
-        this();
-        restConnector.setControllerAddress(address);
-        restConnector.setAdminCredentials(username, password);
-    }
+    private NiciraNvpApi(final Builder builder) {
+        final Map<Class<?>, JsonDeserializer<?>> classToDeserializerMap = new HashMap<>();
+        classToDeserializerMap.put(NatRule.class, new NatRuleAdapter());
+        classToDeserializerMap.put(RoutingConfig.class, new RoutingConfigAdapter());
 
-    public void setControllerAddress(final String address) {
-        restConnector.setControllerAddress(address);
+        final NiciraRestClient niciraRestClient = NiciraRestClient.create()
+            .client(builder.httpClient)
+            .clientContext(builder.httpClientContext)
+            .hostname(builder.host)
+            .username(builder.username)
+            .password(builder.password)
+            .loginUrl(NiciraConstants.LOGIN_URL)
+            .executionLimit(DEFAULT_MAX_RETRIES)
+            .build();
+        restConnector = RESTServiceConnector.create()
+            .classToDeserializerMap(classToDeserializerMap)
+            .client(niciraRestClient)
+            .build();
     }
 
-    public void setAdminCredentials(final String username, final String password) {
-        restConnector.setAdminCredentials(username, password);
+    public static Builder create() {
+        return new Builder();
     }
 
     /**
@@ -123,8 +129,7 @@ public class NiciraNvpApi {
     private <T> T createWithUri(final T entity, final String uri) throws NiciraNvpApiException {
         T createdEntity;
         try {
-            createdEntity = restConnector.executeCreateObject(entity, new TypeToken<T>() {
-            }.getType(), uri, Collections.<String, String> emptyMap());
+            createdEntity = restConnector.executeCreateObject(entity, uri, Collections.<String, String> emptyMap());
         } catch (final CloudstackRESTException e) {
             throw new NiciraNvpApiException(e);
         }
@@ -145,7 +150,7 @@ public class NiciraNvpApi {
         Map<String, String> params = defaultListParams;
         if (uuid.isPresent()) {
             params = new HashMap<String, String>(defaultListParams);
-            params.put("uuid", uuid);
+            params.put(UUID_QUERY_PARAMETER, uuid.get());
         }
 
         NiciraNvpList<T> entities;
@@ -159,7 +164,7 @@ public class NiciraNvpApi {
             throw new NiciraNvpApiException("Unexpected response from API");
         }
 
-        return entities;
+        return entities.getResults();
     }
 
     /**
@@ -231,8 +236,8 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    public NiciraNvpList<SecurityProfile> findSecurityProfile() throws NiciraNvpApiException {
-        return findSecurityProfile(null);
+    public List<SecurityProfile> findSecurityProfile() throws NiciraNvpApiException {
+        return find(ABSENT, SecurityProfile.class);
     }
 
     /**
@@ -244,8 +249,8 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    public NiciraNvpList<SecurityProfile> findSecurityProfile(final String uuid) throws NiciraNvpApiException {
-        return find(uuid, SecurityProfile.class);
+    public List<SecurityProfile> findSecurityProfile(final String uuid) throws NiciraNvpApiException {
+        return find(Optional.fromNullable(uuid), SecurityProfile.class);
     }
 
     /**
@@ -286,7 +291,7 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    public NiciraNvpList<Acl> findAcl() throws NiciraNvpApiException {
+    public List<Acl> findAcl() throws NiciraNvpApiException {
         return findAcl(null);
     }
 
@@ -297,8 +302,8 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    public NiciraNvpList<Acl> findAcl(final String uuid) throws NiciraNvpApiException {
-        return find(uuid, Acl.class);
+    public List<Acl> findAcl(final String uuid) throws NiciraNvpApiException {
+        return find(Optional.fromNullable(uuid), Acl.class);
     }
 
     /**
@@ -332,7 +337,7 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    public NiciraNvpList<LogicalSwitch> findLogicalSwitch() throws NiciraNvpApiException {
+    public List<LogicalSwitch> findLogicalSwitch() throws NiciraNvpApiException {
         return findLogicalSwitch(null);
     }
 
@@ -343,8 +348,8 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    public NiciraNvpList<LogicalSwitch> findLogicalSwitch(final String uuid) throws NiciraNvpApiException {
-        return find(uuid, LogicalSwitch.class);
+    public List<LogicalSwitch> findLogicalSwitch(final String uuid) throws NiciraNvpApiException {
+        return find(Optional.fromNullable(uuid), LogicalSwitch.class);
     }
 
     /**
@@ -363,100 +368,81 @@ public class NiciraNvpApi {
     }
 
     public LogicalSwitchPort createLogicalSwitchPort(final String logicalSwitchUuid, final LogicalSwitchPort logicalSwitchPort) throws NiciraNvpApiException {
-        final String uri = SWITCH_URI_PREFIX + "/" + logicalSwitchUuid + "/lport";
-        return createWithUri(logicalSwitchPort, uri);
+        return createWithUri(logicalSwitchPort, buildLogicalSwitchElementUri(logicalSwitchUuid, LPORT_PATH_SEGMENT));
     }
 
     public void updateLogicalSwitchPort(final String logicalSwitchUuid, final LogicalSwitchPort logicalSwitchPort) throws NiciraNvpApiException {
-        final String uri = SWITCH_URI_PREFIX + "/" + logicalSwitchUuid + "/lport/" + logicalSwitchPort.getUuid();
-        updateWithUri(logicalSwitchPort, uri);
+        updateWithUri(logicalSwitchPort, buildLogicalSwitchElementUri(logicalSwitchUuid, LPORT_PATH_SEGMENT, logicalSwitchPort.getUuid().toString()));
     }
 
     public void updateLogicalSwitchPortAttachment(final String logicalSwitchUuid, final String logicalSwitchPortUuid, final Attachment attachment) throws NiciraNvpApiException {
-        final String uri = SWITCH_URI_PREFIX + "/" + logicalSwitchUuid + "/lport/" + logicalSwitchPortUuid + "/attachment";
-        updateWithUri(attachment, uri);
+        updateWithUri(attachment, buildLogicalSwitchElementUri(logicalSwitchUuid, LPORT_PATH_SEGMENT, logicalSwitchPortUuid) + ATTACHMENT_PATH_SEGMENT);
     }
 
     public void deleteLogicalSwitchPort(final String logicalSwitchUuid, final String logicalSwitchPortUuid) throws NiciraNvpApiException {
-        final String uri = SWITCH_URI_PREFIX + "/" + logicalSwitchUuid + "/lport/" + logicalSwitchPortUuid;
-        deleteWithUri(uri);
+        deleteWithUri(buildLogicalSwitchElementUri(logicalSwitchUuid, LPORT_PATH_SEGMENT, logicalSwitchPortUuid));
     }
 
     public String findLogicalSwitchPortUuidByVifAttachmentUuid(final String logicalSwitchUuid, final String vifAttachmentUuid) throws NiciraNvpApiException {
-        final String uri = SWITCH_URI_PREFIX + "/" + logicalSwitchUuid + "/lport";
-        final Map<String, String> params = new HashMap<String, String>();
-        params.put("attachment_vif_uuid", vifAttachmentUuid);
-        params.put("fields", "uuid");
+        final String uri = buildLogicalSwitchElementUri(logicalSwitchUuid, LPORT_PATH_SEGMENT);
+        final Map<String, String> params = buildBasicParametersMap(UUID_QUERY_PARAMETER);
+        params.put(NiciraConstants.ATTACHMENT_VIF_UUID_QUERY_PARAMETER_NAME, vifAttachmentUuid);
 
-        NiciraNvpList<LogicalSwitchPort> lspl;
+        NiciraNvpList<LogicalSwitchPort> niciraList;
         try {
-            lspl = restConnector.executeRetrieveObject(new TypeToken<NiciraNvpList<LogicalSwitchPort>>() {
-            }.getType(), uri, params);
+            final Type niciraListType = new TypeToken<NiciraNvpList<LogicalSwitchPort>>() {
+            }.getType();
+            niciraList = restConnector.executeRetrieveObject(niciraListType, uri, params);
         } catch (final CloudstackRESTException e) {
             throw new NiciraNvpApiException(e);
         }
 
-        if (lspl == null || lspl.getResultCount() != 1) {
-            throw new NiciraNvpApiException("Unexpected response from API");
+        final List<LogicalSwitchPort> lspl = niciraList.getResults();
+
+        final int listSize = lspl.size();
+        if (listSize != 1) {
+            throw new NiciraNvpApiException("Expected 1 LogicalSwitchPort, but got " + listSize);
         }
 
-        final LogicalSwitchPort lsp = lspl.getResults().get(0);
+        final LogicalSwitchPort lsp = lspl.get(0);
         return lsp.getUuid();
     }
 
     public ControlClusterStatus getControlClusterStatus() throws NiciraNvpApiException {
-        final String uri = "/ws.v1/control-cluster/status";
-        ControlClusterStatus ccs;
+        final String uri = NiciraConstants.CONTROL_CLUSTER_STATUS_URL;
         try {
-            ccs = restConnector.executeRetrieveObject(new TypeToken<ControlClusterStatus>() {
-            }.getType(), uri, null);
+            return restConnector.executeRetrieveObject(ControlClusterStatus.class, uri, new HashMap<String, String>());
         } catch (final CloudstackRESTException e) {
             throw new NiciraNvpApiException(e);
         }
-
-        return ccs;
     }
 
-    public NiciraNvpList<LogicalSwitchPort> findLogicalSwitchPortsByUuid(final String logicalSwitchUuid, final String logicalSwitchPortUuid) throws NiciraNvpApiException {
-        final String uri = SWITCH_URI_PREFIX + "/" + logicalSwitchUuid + "/lport";
-        final Map<String, String> params = new HashMap<String, String>();
-        params.put("uuid", logicalSwitchPortUuid);
-        params.put("fields", "uuid");
+    public List<LogicalSwitchPort> findLogicalSwitchPortsByUuid(final String logicalSwitchUuid, final String logicalSwitchPortUuid) throws NiciraNvpApiException {
+        final String uri = buildLogicalSwitchElementUri(logicalSwitchUuid, LPORT_PATH_SEGMENT);
+        final Map<String, String> params = buildBasicParametersMap(UUID_QUERY_PARAMETER);
+        params.put(UUID_QUERY_PARAMETER, logicalSwitchPortUuid);
 
-        NiciraNvpList<LogicalSwitchPort> lspl;
         try {
-            lspl = restConnector.executeRetrieveObject(new TypeToken<NiciraNvpList<LogicalSwitchPort>>() {
-            }.getType(), uri, params);
+            final Type niciraListType = new TypeToken<NiciraNvpList<LogicalSwitchPort>>() {
+            }.getType();
+            return restConnector.<NiciraNvpList<LogicalSwitchPort>> executeRetrieveObject(niciraListType, uri, params).getResults();
         } catch (final CloudstackRESTException e) {
             throw new NiciraNvpApiException(e);
         }
-
-        if (lspl == null) {
-            throw new NiciraNvpApiException("Unexpected response from API");
-        }
-
-        return lspl;
     }
 
-    public NiciraNvpList<LogicalRouterPort> findLogicalRouterPortsByUuid(final String logicalRouterUuid, final String logicalRouterPortUuid) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/lport";
-        final Map<String, String> params = new HashMap<String, String>();
-        params.put("uuid", logicalRouterPortUuid);
-        params.put("fields", "uuid");
+    public List<LogicalRouterPort> findLogicalRouterPortsByUuid(final String logicalRouterUuid, final String logicalRouterPortUuid) throws NiciraNvpApiException {
+        final String uri = buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT);
+        final Map<String, String> params = buildBasicParametersMap(UUID_QUERY_PARAMETER);
+        params.put(UUID_QUERY_PARAMETER, logicalRouterPortUuid);
 
-        NiciraNvpList<LogicalRouterPort> lrpl;
         try {
-            lrpl = restConnector.executeRetrieveObject(new TypeToken<NiciraNvpList<LogicalRouterPort>>() {
-            }.getType(), uri, params);
+            final Type niciraListType = new TypeToken<NiciraNvpList<LogicalRouterPort>>() {
+            }.getType();
+            return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults();
         } catch (final CloudstackRESTException e) {
             throw new NiciraNvpApiException(e);
         }
-
-        if (lrpl == null) {
-            throw new NiciraNvpApiException("Unexpected response from API");
-        }
-
-        return lrpl;
     }
 
     public LogicalRouter createLogicalRouter(final LogicalRouter logicalRouter) throws NiciraNvpApiException {
@@ -469,7 +455,7 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    public NiciraNvpList<LogicalRouter> findLogicalRouter() throws NiciraNvpApiException {
+    public List<LogicalRouter> findLogicalRouter() throws NiciraNvpApiException {
         return findLogicalRouter(null);
     }
 
@@ -480,12 +466,12 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    public NiciraNvpList<LogicalRouter> findLogicalRouter(final String uuid) throws NiciraNvpApiException {
-        return find(uuid, LogicalRouter.class);
+    public List<LogicalRouter> findLogicalRouter(final String uuid) throws NiciraNvpApiException {
+        return find(Optional.fromNullable(uuid), LogicalRouter.class);
     }
 
     public LogicalRouter findOneLogicalRouterByUuid(final String logicalRouterUuid) throws NiciraNvpApiException {
-        return findLogicalRouter(logicalRouterUuid).getResults().get(0);
+        return findLogicalRouter(logicalRouterUuid).get(0);
     }
 
     public void updateLogicalRouter(final LogicalRouter logicalRouter, final String logicalRouterUuid) throws NiciraNvpApiException {
@@ -493,100 +479,149 @@ public class NiciraNvpApi {
     }
 
     public void deleteLogicalRouter(final String logicalRouterUuid) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid;
-        deleteWithUri(uri);
+        deleteWithUri(buildLogicalRouterUri(logicalRouterUuid));
     }
 
     public LogicalRouterPort createLogicalRouterPort(final String logicalRouterUuid, final LogicalRouterPort logicalRouterPort) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/lport";
-        return createWithUri(logicalRouterPort, uri);
+        return createWithUri(logicalRouterPort, buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT));
     }
 
     public void deleteLogicalRouterPort(final String logicalRouterUuid, final String logicalRouterPortUuid) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/lport/" + logicalRouterPortUuid;
-        deleteWithUri(uri);
+        deleteWithUri(buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT, logicalRouterPortUuid));
     }
 
     public void updateLogicalRouterPort(final String logicalRouterUuid, final LogicalRouterPort logicalRouterPort) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/lport/" + logicalRouterPort.getUuid();
-        updateWithUri(logicalRouterPort, uri);
+        updateWithUri(logicalRouterPort, buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT, logicalRouterPort.getUuid().toString()));
     }
 
     public void updateLogicalRouterPortAttachment(final String logicalRouterUuid, final String logicalRouterPortUuid, final Attachment attachment) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/lport/" + logicalRouterPortUuid + "/attachment";
-        updateWithUri(attachment, uri);
+        updateWithUri(attachment, buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT, logicalRouterPortUuid) + ATTACHMENT_PATH_SEGMENT);
     }
 
     public NatRule createLogicalRouterNatRule(final String logicalRouterUuid, final NatRule natRule) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/nat";
-        return createWithUri(natRule, uri);
+        return createWithUri(natRule, buildLogicalRouterElementUri(logicalRouterUuid, NAT_PATH_SEGMENT));
     }
 
     public void updateLogicalRouterNatRule(final String logicalRouterUuid, final NatRule natRule) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/nat/" + natRule.getUuid();
-        updateWithUri(natRule, uri);
+        updateWithUri(natRule, buildLogicalRouterElementUri(logicalRouterUuid, NAT_PATH_SEGMENT, natRule.getUuid().toString()));
     }
 
     public void deleteLogicalRouterNatRule(final String logicalRouterUuid, final UUID natRuleUuid) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/nat/" + natRuleUuid.toString();
-        deleteWithUri(uri);
+        deleteWithUri(buildLogicalRouterElementUri(logicalRouterUuid, NAT_PATH_SEGMENT, natRuleUuid.toString()));
     }
 
-    public NiciraNvpList<LogicalRouterPort> findLogicalRouterPortByGatewayServiceAndVlanId(final String logicalRouterUuid, final String gatewayServiceUuid, final long vlanId)
-            throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/lport";
-        final Map<String, String> params = new HashMap<String, String>();
-        params.put("attachment_gwsvc_uuid", gatewayServiceUuid);
-        params.put("attachment_vlan", Long.toString(vlanId));
-        params.put("fields", "*");
+    public List<LogicalRouterPort> findLogicalRouterPortByGatewayServiceAndVlanId(final String logicalRouterUuid, final String gatewayServiceUuid, final long vlanId)
+                    throws NiciraNvpApiException {
+        final String uri = buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT);
+        final Map<String, String> params = buildBasicParametersMap(WILDCARD_QUERY_PARAMETER);
+        params.put(ATTACHMENT_GWSVC_UUID_QUERY_PARAMETER, gatewayServiceUuid);
+        params.put(NiciraConstants.ATTACHMENT_VLAN_PARAMETER, Long.toString(vlanId));
 
         try {
-            return restConnector.executeRetrieveObject(new TypeToken<NiciraNvpList<LogicalRouterPort>>() {
-            }.getType(), uri, params);
+            final Type niciraListType = new TypeToken<NiciraNvpList<LogicalRouterPort>>() {
+            }.getType();
+            return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults();
         } catch (final CloudstackRESTException e) {
             throw new NiciraNvpApiException(e);
         }
     }
 
-    public NiciraNvpList<NatRule> findNatRulesByLogicalRouterUuid(final String logicalRouterUuid) throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/nat";
-        final Map<String, String> params = new HashMap<String, String>();
-        params.put("fields", "*");
+    public List<NatRule> findNatRulesByLogicalRouterUuid(final String logicalRouterUuid) throws NiciraNvpApiException {
+        final String uri = buildLogicalRouterElementUri(logicalRouterUuid, NAT_PATH_SEGMENT);
+        final Map<String, String> params = buildBasicParametersMap(WILDCARD_QUERY_PARAMETER);
 
         try {
-            return restConnector.executeRetrieveObject(new TypeToken<NiciraNvpList<NatRule>>() {
-            }.getType(), uri, params);
+            final Type niciraListType = new TypeToken<NiciraNvpList<NatRule>>() {
+            }.getType();
+            return restConnector.<NiciraNvpList<NatRule>> executeRetrieveObject(niciraListType, uri, params).getResults();
         } catch (final CloudstackRESTException e) {
             throw new NiciraNvpApiException(e);
         }
     }
 
-    public NiciraNvpList<LogicalRouterPort> findLogicalRouterPortByGatewayServiceUuid(final String logicalRouterUuid, final String l3GatewayServiceUuid)
-            throws NiciraNvpApiException {
-        final String uri = ROUTER_URI_PREFIX + "/" + logicalRouterUuid + "/lport";
-        final Map<String, String> params = new HashMap<String, String>();
-        params.put("fields", "*");
-        params.put("attachment_gwsvc_uuid", l3GatewayServiceUuid);
+    public List<LogicalRouterPort> findLogicalRouterPortByGatewayServiceUuid(final String logicalRouterUuid, final String l3GatewayServiceUuid)
+                    throws NiciraNvpApiException {
+        final String uri = buildLogicalRouterElementUri(logicalRouterUuid, LPORT_PATH_SEGMENT);
+        final Map<String, String> params = buildBasicParametersMap(WILDCARD_QUERY_PARAMETER);
+        params.put(ATTACHMENT_GWSVC_UUID_QUERY_PARAMETER, l3GatewayServiceUuid);
 
         try {
-            return restConnector.executeRetrieveObject(new TypeToken<NiciraNvpList<LogicalRouterPort>>() {
-            }.getType(), uri, params);
+            final Type niciraListType = new TypeToken<NiciraNvpList<LogicalRouterPort>>() {
+            }.getType();
+            return restConnector.<NiciraNvpList<LogicalRouterPort>> executeRetrieveObject(niciraListType, uri, params).getResults();
         } catch (final CloudstackRESTException e) {
             throw new NiciraNvpApiException(e);
         }
     }
 
+    private static Map<String, String> buildBasicParametersMap(final String fieldsQueryValue) {
+        final Map<String, String> params = new HashMap<String, String>();
+        params.put(FIELDS_QUERY_PARAMETER, fieldsQueryValue);
+        return params;
+    }
 
+    private static String buildUri(final String uriPrefix, final String uuid) {
+        return uriPrefix + "/" + uuid;
+    }
 
+    private static String buildLogicalSwitchUri(final String logicalSwitchUuid) {
+        return buildUri(SWITCH_URI_PREFIX, logicalSwitchUuid);
+    }
 
+    private static String buildLogicalSwitchElementUri(final String logicalSwitchUuid, final String logicalElementType) {
+        return buildLogicalSwitchUri(logicalSwitchUuid) + logicalElementType;
+    }
 
+    private static String buildLogicalSwitchElementUri(final String logicalSwitchUuid, final String logicalElementType, final String elementUuid) {
+        return buildLogicalSwitchElementUri(logicalSwitchUuid, logicalElementType) + "/" + elementUuid.toString();
     }
 
+    private static String buildLogicalRouterUri(final String logicalRouterUuid) {
+        return buildUri(ROUTER_URI_PREFIX, logicalRouterUuid);
+    }
 
+    private static String buildLogicalRouterElementUri(final String logicalRouterUuid, final String logicalElementType) {
+        return buildLogicalRouterUri(logicalRouterUuid) + logicalElementType;
+    }
+
+    private static String buildLogicalRouterElementUri(final String logicalRouterUuid, final String logicalRouterElementType, final String elementUuid) {
+        return buildLogicalRouterElementUri(logicalRouterUuid, logicalRouterElementType) + "/" + elementUuid.toString();
+    }
 
+    public static class Builder {
+        private String host;
+        private String username;
+        private String password;
+        private CloseableHttpClient httpClient;
+        private HttpClientContext httpClientContext = HttpClientContext.create();
 
+        public Builder host(final String host) {
+            this.host = host;
+            return this;
+        }
+
+        public Builder username(final String username) {
+            this.username = username;
+            return this;
+        }
 
+        public Builder password(final String password) {
+            this.password = password;
+            return this;
+        }
+
+        public Builder httpClient(final CloseableHttpClient httpClient) {
+            this.httpClient = httpClient;
+            return this;
+        }
+
+        public Builder httpClientContext(final HttpClientContext httpClientContext) {
+            this.httpClientContext = httpClientContext;
+            return this;
+        }
 
+        public NiciraNvpApi build() {
+            return new NiciraNvpApi(this);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpResource.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpResource.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpResource.java
index af5812d..d6355b2 100644
--- a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpResource.java
+++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/NiciraNvpResource.java
@@ -19,6 +19,9 @@
 
 package com.cloud.network.resource;
 
+import java.security.KeyManagementException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
 import java.util.Map;
 
 import javax.naming.ConfigurationException;
@@ -42,6 +45,8 @@ import com.cloud.network.nicira.NiciraNvpApiException;
 import com.cloud.network.nicira.SourceNatRule;
 import com.cloud.network.utils.CommandRetryUtility;
 import com.cloud.resource.ServerResource;
+import com.cloud.utils.rest.CloudstackRESTException;
+import com.cloud.utils.rest.HttpClientHelper;
 
 public class NiciraNvpResource implements ServerResource {
 
@@ -49,6 +54,7 @@ public class NiciraNvpResource implements ServerResource {
 
     public static final int NAME_MAX_LEN = 40;
     public static final int NUM_RETRIES = 2;
+    private static final int MAX_REDIRECTS = 5;
 
     private String name;
     private String guid;
@@ -58,8 +64,16 @@ public class NiciraNvpResource implements ServerResource {
     private NiciraNvpUtilities niciraNvpUtilities;
     private CommandRetryUtility retryUtility;
 
-    protected NiciraNvpApi createNiciraNvpApi() {
-        return new NiciraNvpApi();
+    protected NiciraNvpApi createNiciraNvpApi(final String host, final String username, final String password) throws CloudstackRESTException {
+        try {
+            return NiciraNvpApi.create().host(host).username(username).password(password).httpClient(HttpClientHelper.createHttpClient(MAX_REDIRECTS)).build();
+        } catch (final KeyManagementException e) {
+            throw new CloudstackRESTException("Could not create HTTP client", e);
+        } catch (final NoSuchAlgorithmException e) {
+            throw new CloudstackRESTException("Could not create HTTP client", e);
+        } catch (final KeyStoreException e) {
+            throw new CloudstackRESTException("Could not create HTTP client", e);
+        }
     }
 
     @Override
@@ -99,9 +113,11 @@ public class NiciraNvpResource implements ServerResource {
         retryUtility = CommandRetryUtility.getInstance();
         retryUtility.setServerResource(this);
 
-        niciraNvpApi = createNiciraNvpApi();
-        niciraNvpApi.setControllerAddress(ip);
-        niciraNvpApi.setAdminCredentials(adminuser, adminpass);
+        try {
+            niciraNvpApi = createNiciraNvpApi(ip, adminuser, adminpass);
+        } catch (final CloudstackRESTException e) {
+            throw new ConfigurationException("Could not create a Nicira Nvp API client: " + e.getMessage());
+        }
 
         return true;
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePortForwardingRulesCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePortForwardingRulesCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePortForwardingRulesCommandWrapper.java
index 35ab04c..ae0d4ba 100644
--- a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePortForwardingRulesCommandWrapper.java
+++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePortForwardingRulesCommandWrapper.java
@@ -21,6 +21,8 @@ package com.cloud.network.resource.wrapper;
 
 import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.api.Answer;
@@ -30,7 +32,6 @@ import com.cloud.agent.api.to.PortForwardingRuleTO;
 import com.cloud.network.nicira.NatRule;
 import com.cloud.network.nicira.NiciraNvpApi;
 import com.cloud.network.nicira.NiciraNvpApiException;
-import com.cloud.network.nicira.NiciraNvpList;
 import com.cloud.network.resource.NiciraNvpResource;
 import com.cloud.network.utils.CommandRetryUtility;
 import com.cloud.resource.CommandWrapper;
@@ -45,7 +46,7 @@ public final class NiciraNvpConfigurePortForwardingRulesCommandWrapper extends C
     public Answer execute(final ConfigurePortForwardingRulesOnLogicalRouterCommand command, final NiciraNvpResource niciraNvpResource) {
         final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
         try {
-            final NiciraNvpList<NatRule> existingRules = niciraNvpApi.findNatRulesByLogicalRouterUuid(command.getLogicalRouterUuid());
+            final List<NatRule> existingRules = niciraNvpApi.findNatRulesByLogicalRouterUuid(command.getLogicalRouterUuid());
             // Rules of the game (also known as assumptions-that-will-make-stuff-break-later-on)
             // A SourceNat rule with a match other than a /32 cidr is assumed to be the "main" SourceNat rule
             // Any other SourceNat rule should have a corresponding DestinationNat rule
@@ -66,7 +67,7 @@ public final class NiciraNvpConfigurePortForwardingRulesCommandWrapper extends C
                 NatRule incoming = null;
                 NatRule outgoing = null;
 
-                for (final NatRule storedRule : existingRules.getResults()) {
+                for (final NatRule storedRule : existingRules) {
                     if (storedRule.equalsIgnoreUuid(rulepair[1])) {
                         // The outgoing rule exists
                         outgoing = storedRule;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePublicIpsCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePublicIpsCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePublicIpsCommandWrapper.java
index 9d4c711..d584629 100644
--- a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePublicIpsCommandWrapper.java
+++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigurePublicIpsCommandWrapper.java
@@ -21,13 +21,14 @@ package com.cloud.network.resource.wrapper;
 
 import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES;
 
+import java.util.List;
+
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterAnswer;
 import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand;
 import com.cloud.network.nicira.LogicalRouterPort;
 import com.cloud.network.nicira.NiciraNvpApi;
 import com.cloud.network.nicira.NiciraNvpApiException;
-import com.cloud.network.nicira.NiciraNvpList;
 import com.cloud.network.resource.NiciraNvpResource;
 import com.cloud.network.utils.CommandRetryUtility;
 import com.cloud.resource.CommandWrapper;
@@ -41,11 +42,11 @@ public final class NiciraNvpConfigurePublicIpsCommandWrapper extends CommandWrap
         final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
 
         try {
-            final NiciraNvpList<LogicalRouterPort> ports = niciraNvpApi.findLogicalRouterPortByGatewayServiceUuid(command.getLogicalRouterUuid(), command.getL3GatewayServiceUuid());
-            if (ports.getResultCount() != 1) {
+            final List<LogicalRouterPort> ports = niciraNvpApi.findLogicalRouterPortByGatewayServiceUuid(command.getLogicalRouterUuid(), command.getL3GatewayServiceUuid());
+            if (ports.size() != 1) {
                 return new ConfigurePublicIpsOnLogicalRouterAnswer(command, false, "No logical router ports found, unable to set ip addresses");
             }
-            final LogicalRouterPort lrp = ports.getResults().get(0);
+            final LogicalRouterPort lrp = ports.get(0);
             lrp.setIpAddresses(command.getPublicCidrs());
             niciraNvpApi.updateLogicalRouterPort(command.getLogicalRouterUuid(), lrp);
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureStaticNatRulesCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureStaticNatRulesCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureStaticNatRulesCommandWrapper.java
index e576db9..ae59784 100644
--- a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureStaticNatRulesCommandWrapper.java
+++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpConfigureStaticNatRulesCommandWrapper.java
@@ -21,6 +21,8 @@ package com.cloud.network.resource.wrapper;
 
 import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.api.Answer;
@@ -30,7 +32,6 @@ import com.cloud.agent.api.to.StaticNatRuleTO;
 import com.cloud.network.nicira.NatRule;
 import com.cloud.network.nicira.NiciraNvpApi;
 import com.cloud.network.nicira.NiciraNvpApiException;
-import com.cloud.network.nicira.NiciraNvpList;
 import com.cloud.network.resource.NiciraNvpResource;
 import com.cloud.network.utils.CommandRetryUtility;
 import com.cloud.resource.CommandWrapper;
@@ -46,7 +47,7 @@ public final class NiciraNvpConfigureStaticNatRulesCommandWrapper extends Comman
         final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
 
         try {
-            final NiciraNvpList<NatRule> existingRules = niciraNvpApi.findNatRulesByLogicalRouterUuid(command.getLogicalRouterUuid());
+            final List<NatRule> existingRules = niciraNvpApi.findNatRulesByLogicalRouterUuid(command.getLogicalRouterUuid());
             // Rules of the game (also known as assumptions-that-will-make-stuff-break-later-on)
             // A SourceNat rule with a match other than a /32 cidr is assumed to be the "main" SourceNat rule
             // Any other SourceNat rule should have a corresponding DestinationNat rule
@@ -58,7 +59,7 @@ public final class NiciraNvpConfigureStaticNatRulesCommandWrapper extends Comman
                 NatRule incoming = null;
                 NatRule outgoing = null;
 
-                for (final NatRule storedRule : existingRules.getResults()) {
+                for (final NatRule storedRule : existingRules) {
                     if (storedRule.equalsIgnoreUuid(rulepair[1])) {
                         // The outgoing rule exists
                         outgoing = storedRule;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpFindLogicalSwitchPortCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpFindLogicalSwitchPortCommandWrapper.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpFindLogicalSwitchPortCommandWrapper.java
index ea68538..8c11427 100644
--- a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpFindLogicalSwitchPortCommandWrapper.java
+++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/resource/wrapper/NiciraNvpFindLogicalSwitchPortCommandWrapper.java
@@ -21,13 +21,14 @@ package com.cloud.network.resource.wrapper;
 
 import static com.cloud.network.resource.NiciraNvpResource.NUM_RETRIES;
 
+import java.util.List;
+
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.FindLogicalSwitchPortAnswer;
 import com.cloud.agent.api.FindLogicalSwitchPortCommand;
 import com.cloud.network.nicira.LogicalSwitchPort;
 import com.cloud.network.nicira.NiciraNvpApi;
 import com.cloud.network.nicira.NiciraNvpApiException;
-import com.cloud.network.nicira.NiciraNvpList;
 import com.cloud.network.resource.NiciraNvpResource;
 import com.cloud.network.utils.CommandRetryUtility;
 import com.cloud.resource.CommandWrapper;
@@ -44,8 +45,8 @@ public final class NiciraNvpFindLogicalSwitchPortCommandWrapper extends CommandW
         final NiciraNvpApi niciraNvpApi = niciraNvpResource.getNiciraNvpApi();
 
         try {
-            final NiciraNvpList<LogicalSwitchPort> ports = niciraNvpApi.findLogicalSwitchPortsByUuid(logicalSwitchUuid, logicalSwitchPortUuid);
-            if (ports.getResultCount() == 0) {
+            final List<LogicalSwitchPort> ports = niciraNvpApi.findLogicalSwitchPortsByUuid(logicalSwitchUuid, logicalSwitchPortUuid);
+            if (ports.size() == 0) {
                 return new FindLogicalSwitchPortAnswer(command, false, "Logical switchport " + logicalSwitchPortUuid + " not found", null);
             } else {
                 return new FindLogicalSwitchPortAnswer(command, true, "Logical switchport " + logicalSwitchPortUuid + " found", logicalSwitchPortUuid);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiIT.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiIT.java b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiIT.java
index 3bb4b9d..a95a8d6 100644
--- a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiIT.java
+++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiIT.java
@@ -22,16 +22,15 @@ package com.cloud.network.nicira;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Properties;
 import java.util.UUID;
 
 import org.junit.Before;
 import org.junit.Test;
 
 import com.cloud.utils.PropertiesUtil;
+import com.cloud.utils.rest.HttpClientHelper;
 
 public class NiciraNvpApiIT {
 
@@ -40,18 +39,23 @@ public class NiciraNvpApiIT {
     protected long timestamp = System.currentTimeMillis();
 
     @Before
-    public void setup() throws IOException {
-        final Properties properties = PropertiesUtil.loadFromFile(PropertiesUtil.findConfigFile("config.properties"));
-        api = new NiciraNvpApi();
-        api.setControllerAddress(properties.getProperty("nvp.host"));
-        api.setAdminCredentials(properties.getProperty("nvp.admin.user"),
-                properties.getProperty("nvp.admin.pwd"));
+    public void setup() throws Exception {
+        PropertiesUtil.loadFromFile(PropertiesUtil.findConfigFile("config.properties"));
+        final String host = System.getProperty("nvp.host");
+        final String user = System.getProperty("nvp.admin.user");
+        final String pass = System.getProperty("nvp.admin.pwd");
+        api = NiciraNvpApi.create()
+            .host(host)
+            .username(user)
+            .password(pass)
+            .httpClient(HttpClientHelper.createHttpClient(5))
+            .build();
     }
 
     @Test
-    public void testCRUDSecurityProfile() throws NiciraNvpApiException {
+    public void testCRUDSecurityProfile() {
         SecurityProfile sProfile = new SecurityProfile();
-        sProfile.setDisplayName("SecProfile"+timestamp);
+        sProfile.setDisplayName("SecProfile" + timestamp);
 
         final List<SecurityRule> egressRules = new ArrayList<SecurityRule>();
         sProfile.setLogicalPortEgressRules(egressRules);
@@ -73,27 +77,23 @@ public class NiciraNvpApiIT {
             sProfile = api.createSecurityProfile(sProfile);
 
             // We can now update the new entity
-            sProfile.setDisplayName("UpdatedSecProfile"+timestamp);
+            sProfile.setDisplayName("UpdatedSecProfile" + timestamp);
             api.updateSecurityProfile(sProfile, sProfile.getUuid());
 
             // Read them all
-            NiciraNvpList<SecurityProfile> profiles = api.findSecurityProfile();
+            List<SecurityProfile> profiles = api.findSecurityProfile();
             SecurityProfile scInList = null;
-            for(final SecurityProfile iProfile : profiles.getResults()) {
+            for (final SecurityProfile iProfile : profiles) {
                 if (iProfile.getUuid().equalsIgnoreCase(sProfile.getUuid())) {
                     scInList = iProfile;
                 }
             }
-            assertEquals("Read a Security Profile different from the one just created and updated",
-                    sProfile, scInList);
+            assertEquals("Read a Security Profile different from the one just created and updated", sProfile, scInList);
 
             // Read them filtered by uuid (get one)
             profiles = api.findSecurityProfile(sProfile.getUuid());
-            assertEquals("Read a Security Profile different from the one just created and updated",
-                    sProfile,
-                    profiles.getResults().get(0));
-            assertEquals("Read a Security Profile filtered by unique id (UUID) with more than one item",
-                    1, profiles.getResults().size());
+            assertEquals("Read a Security Profile different from the one just created and updated", sProfile, profiles.get(0));
+            assertEquals("Read a Security Profile filtered by unique id (UUID) with more than one item", 1, profiles.size());
 
             // We can now delete the new entity
             api.deleteSecurityProfile(sProfile.getUuid());
@@ -104,25 +104,21 @@ public class NiciraNvpApiIT {
     }
 
     @Test
-    public void testCRUDAcl() throws NiciraNvpApiException {
+    public void testCRUDAcl() {
         Acl acl = new Acl();
-        acl.setDisplayName("Acl"+timestamp);
+        acl.setDisplayName("Acl" + timestamp);
 
         // Note that if the protocol is 6 (TCP) then you cannot put ICMP code and type
         // Note that if the protocol is 1 (ICMP) then you cannot put ports
         final List<AclRule> egressRules = new ArrayList<AclRule>();
         acl.setLogicalPortEgressRules(egressRules);
-        egressRules.add(new AclRule(AclRule.ETHERTYPE_IPV4, 1, "allow", null, null,
-                "1.10.10.0", "1.10.10.1", null, null, null, null, 0, 0, 5));
-        egressRules.add(new AclRule(AclRule.ETHERTYPE_IPV4, 6, "allow", null, null,
-                "1.10.10.6", "1.10.10.7", 80, 80, 80, 80, 1, null, null));
+        egressRules.add(new AclRule(AclRule.ETHERTYPE_IPV4, 1, "allow", null, null, "1.10.10.0", "1.10.10.1", null, null, null, null, 0, 0, 5));
+        egressRules.add(new AclRule(AclRule.ETHERTYPE_IPV4, 6, "allow", null, null, "1.10.10.6", "1.10.10.7", 80, 80, 80, 80, 1, null, null));
 
         final List<AclRule> ingressRules = new ArrayList<AclRule>();
         acl.setLogicalPortIngressRules(ingressRules);
-        ingressRules.add(new AclRule(AclRule.ETHERTYPE_IPV4, 1, "allow", null, null,
-                "1.10.10.0", "1.10.10.1", null, null, null, null, 0, 0, 5));
-        ingressRules.add(new AclRule(AclRule.ETHERTYPE_IPV4, 6, "allow", null, null,
-                "1.10.10.6", "1.10.10.7", 80, 80, 80, 80, 1, null, null));
+        ingressRules.add(new AclRule(AclRule.ETHERTYPE_IPV4, 1, "allow", null, null, "1.10.10.0", "1.10.10.1", null, null, null, null, 0, 0, 5));
+        ingressRules.add(new AclRule(AclRule.ETHERTYPE_IPV4, 6, "allow", null, null, "1.10.10.6", "1.10.10.7", 80, 80, 80, 80, 1, null, null));
 
         final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
         acl.setTags(tags);
@@ -134,27 +130,23 @@ public class NiciraNvpApiIT {
             acl = api.createAcl(acl);
 
             // We can now update the new entity
-            acl.setDisplayName("UpdatedAcl"+timestamp);
+            acl.setDisplayName("UpdatedAcl" + timestamp);
             api.updateAcl(acl, acl.getUuid());
 
             // Read them all
-            NiciraNvpList<Acl> acls = api.findAcl();
+            List<Acl> acls = api.findAcl();
             Acl scInList = null;
-            for(final Acl iAcl : acls.getResults()) {
+            for (final Acl iAcl : acls) {
                 if (iAcl.getUuid().equalsIgnoreCase(acl.getUuid())) {
                     scInList = iAcl;
                 }
             }
-            assertEquals("Read a ACL different from the one just created and updated",
-                    acl, scInList);
+            assertEquals("Read a ACL different from the one just created and updated", acl, scInList);
 
             // Read them filtered by uuid (get one)
             acls = api.findAcl(acl.getUuid());
-            assertEquals("Read a ACL different from the one just created and updated",
-                    acl,
-                    acls.getResults().get(0));
-            assertEquals("Read a ACL filtered by unique id (UUID) with more than one item",
-                    1, acls.getResults().size());
+            assertEquals("Read a ACL different from the one just created and updated", acl, acls.get(0));
+            assertEquals("Read a ACL filtered by unique id (UUID) with more than one item", 1, acls.size());
 
             // We can now delete the new entity
             api.deleteAcl(acl.getUuid());
@@ -165,9 +157,9 @@ public class NiciraNvpApiIT {
     }
 
     @Test
-    public void testCRUDLogicalSwitch() throws NiciraNvpApiException {
+    public void testCRUDLogicalSwitch() throws Exception {
         LogicalSwitch logicalSwitch = new LogicalSwitch();
-        logicalSwitch.setDisplayName("LogicalSwitch"+timestamp);
+        logicalSwitch.setDisplayName("LogicalSwitch" + timestamp);
         logicalSwitch.setPortIsolationEnabled(true);
         logicalSwitch.setReplicationMode("service");
         logicalSwitch.setTags(new ArrayList<NiciraNvpTag>());
@@ -175,78 +167,65 @@ public class NiciraNvpApiIT {
 
         // In the creation we don't get to specify UUID, href or schema: they don't exist yet
 
-        try {
-            logicalSwitch = api.createLogicalSwitch(logicalSwitch);
+        logicalSwitch = api.createLogicalSwitch(logicalSwitch);
 
-            // We can now update the new entity
-            logicalSwitch.setDisplayName("UpdatedLogicalSwitch"+timestamp);
-            api.updateLogicalSwitch(logicalSwitch, logicalSwitch.getUuid());
+        // We can now update the new entity
+        logicalSwitch.setDisplayName("UpdatedLogicalSwitch" + timestamp);
+        api.updateLogicalSwitch(logicalSwitch, logicalSwitch.getUuid());
 
-            // Read them all
-            NiciraNvpList<LogicalSwitch> logicalSwitches = api.findLogicalSwitch();
-            for(final LogicalSwitch iLogicalSwitch : logicalSwitches.getResults()) {
-                if (iLogicalSwitch.getUuid().equalsIgnoreCase(logicalSwitch.getUuid())) {
-                    assertEquals("Read a LogicalSwitch different from the one just created and updated",
-                            logicalSwitch, iLogicalSwitch);
-                }
+        // Read them all
+        List<LogicalSwitch> logicalSwitches = api.findLogicalSwitch();
+        for (final LogicalSwitch iLogicalSwitch : logicalSwitches) {
+            if (iLogicalSwitch.getUuid().equalsIgnoreCase(logicalSwitch.getUuid())) {
+                assertEquals("Read a LogicalSwitch different from the one just created and updated", logicalSwitch, iLogicalSwitch);
             }
+        }
 
-            // Read them filtered by uuid (get one)
-            logicalSwitches = api.findLogicalSwitch(logicalSwitch.getUuid());
-            assertEquals("Read a LogicalSwitch different from the one just created and updated",
-                    logicalSwitch,
-                    logicalSwitches.getResults().get(0));
-            assertEquals("Read a LogicalSwitch filtered by unique id (UUID) with more than one item",
-                    1, logicalSwitches.getResults().size());
-
-            // Before deleting the test LogicalSwitch, test its ports
-            final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
-            tags.add(new NiciraNvpTag("cs_account", "OwnerName"));
+        // Read them filtered by uuid (get one)
+        logicalSwitches = api.findLogicalSwitch(logicalSwitch.getUuid());
+        assertEquals("Read a LogicalSwitch different from the one just created and updated", logicalSwitch, logicalSwitches.get(0));
+        assertEquals("Read a LogicalSwitch filtered by unique id (UUID) with more than one item", 1, logicalSwitches.size());
 
-            LogicalSwitchPort logicalSwitchPort = new LogicalSwitchPort("LSwitchPort"+timestamp, tags, true);
-            logicalSwitchPort = api.createLogicalSwitchPort(logicalSwitch.getUuid(), logicalSwitchPort);
+        // Before deleting the test LogicalSwitch, test its ports
+        final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
+        tags.add(new NiciraNvpTag("cs_account", "OwnerName"));
 
-            logicalSwitchPort.setDisplayName("UpdatedLSwitchPort"+timestamp);
-            api.updateLogicalSwitchPort(logicalSwitch.getUuid(), logicalSwitchPort);
+        LogicalSwitchPort logicalSwitchPort = new LogicalSwitchPort("LSwitchPort" + timestamp, tags, true);
+        logicalSwitchPort = api.createLogicalSwitchPort(logicalSwitch.getUuid(), logicalSwitchPort);
 
-            final NiciraNvpList<LogicalSwitchPort> logicalSwitchePorts =
-                    api.findLogicalSwitchPortsByUuid(logicalSwitch.getUuid(), logicalSwitchPort.getUuid());
-            for(final LogicalSwitchPort iLSwitchPort : logicalSwitchePorts.getResults()) {
-                if (iLSwitchPort.getUuid().equalsIgnoreCase(logicalSwitchPort.getUuid())) {
-                    assertEquals("Read a LogicalSwitchPort different from the one just created and updated",
-                            logicalSwitchPort, iLSwitchPort);
-                }
+        logicalSwitchPort.setDisplayName("UpdatedLSwitchPort" + timestamp);
+        api.updateLogicalSwitchPort(logicalSwitch.getUuid(), logicalSwitchPort);
+
+        final List<LogicalSwitchPort> logicalSwitchePorts = api.findLogicalSwitchPortsByUuid(logicalSwitch.getUuid(), logicalSwitchPort.getUuid());
+        for (final LogicalSwitchPort iLSwitchPort : logicalSwitchePorts) {
+            if (iLSwitchPort.getUuid().equalsIgnoreCase(logicalSwitchPort.getUuid())) {
+                assertEquals("Read a LogicalSwitchPort different from the one just created and updated", logicalSwitchPort, iLSwitchPort);
             }
+        }
 
-            // And finally test attachments
-            final String attachmentUuid = UUID.randomUUID().toString();
-            final VifAttachment vifAttachment = new VifAttachment(attachmentUuid);
-            api.updateLogicalSwitchPortAttachment(logicalSwitch.getUuid(), logicalSwitchPort.getUuid(),
-                    vifAttachment);
+        // And finally test attachments
+        final String attachmentUuid = UUID.randomUUID().toString();
+        final VifAttachment vifAttachment = new VifAttachment(attachmentUuid);
+        api.updateLogicalSwitchPortAttachment(logicalSwitch.getUuid(), logicalSwitchPort.getUuid(), vifAttachment);
 
-            assertEquals("Read a LogicalSwitchPort by vifAttachment different than expected",
-                    api.findLogicalSwitchPortUuidByVifAttachmentUuid(logicalSwitch.getUuid(), vifAttachment.getVifUuid()),
-                            logicalSwitchPort.getUuid());
+        assertEquals("Read a LogicalSwitchPort by vifAttachment different than expected",
+                        api.findLogicalSwitchPortUuidByVifAttachmentUuid(logicalSwitch.getUuid(), vifAttachment.getVifUuid()), logicalSwitchPort.getUuid());
 
-            api.deleteLogicalSwitchPort(logicalSwitch.getUuid(), logicalSwitchPort.getUuid());
+        api.deleteLogicalSwitchPort(logicalSwitch.getUuid(), logicalSwitchPort.getUuid());
 
-            // We can now delete the new entity
-            api.deleteLogicalSwitch(logicalSwitch.getUuid());
-        } catch (final NiciraNvpApiException e) {
-            e.printStackTrace();
-            assertTrue("Errors in LogicalSwitch CRUD", false);
-        }
+        // We can now delete the new entity
+        api.deleteLogicalSwitch(logicalSwitch.getUuid());
     }
 
     @Test
-    public void testCRUDLogicalRouter() throws NiciraNvpApiException {
+    public void testCRUDLogicalRouter() {
         LogicalRouter logicalRouter = new LogicalRouter();
-        logicalRouter.setDisplayName("LogicalRouter"+timestamp);
+        logicalRouter.setDisplayName("LogicalRouter" + timestamp);
         logicalRouter.setDistributed(true);
         logicalRouter.setNatSynchronizationEnabled(true);
         logicalRouter.setReplicationMode(LogicalRouter.REPLICATION_MODE_SERVICE);
         final RoutingConfig routingConfig = new SingleDefaultRouteImplicitRoutingConfig(
-                new RouterNextHop("192.168.10.20"));
+                        new RouterNextHop("192.168.10.20"));
         logicalRouter.setRoutingConfig(routingConfig);
 
         // In the creation we don't get to specify UUID, href or schema: they don't exist yet
@@ -255,37 +234,32 @@ public class NiciraNvpApiIT {
             logicalRouter = api.createLogicalRouter(logicalRouter);
 
             // We can now update the new entity
-            logicalRouter.setDisplayName("UpdatedLogicalSwitch"+timestamp);
+            logicalRouter.setDisplayName("UpdatedLogicalSwitch" + timestamp);
             api.updateLogicalRouter(logicalRouter, logicalRouter.getUuid());
 
             // Read them all
-            NiciraNvpList<LogicalRouter> logicalRouters = api.findLogicalRouter();
+            List<LogicalRouter> logicalRouters = api.findLogicalRouter();
             LogicalRouter lsInList = null;
-            for(final LogicalRouter iLogicalRouter : logicalRouters.getResults()) {
+            for (final LogicalRouter iLogicalRouter : logicalRouters) {
                 if (iLogicalRouter.getUuid().equalsIgnoreCase(logicalRouter.getUuid())) {
                     lsInList = iLogicalRouter;
                 }
             }
-            assertEquals("Read a LogicalRouter different from the one just created and updated",
-                    logicalRouter, lsInList);
+            assertEquals("Read a LogicalRouter different from the one just created and updated", logicalRouter, lsInList);
 
             // Read them filtered by uuid (get one)
             logicalRouters = api.findLogicalRouter(logicalRouter.getUuid());
-            assertEquals("Read a LogicalRouter different from the one just created and updated",
-                    logicalRouter,
-                    logicalRouters.getResults().get(0));
-            assertEquals("Read a LogicalRouter filtered by unique id (UUID) with more than one item",
-                    1, logicalRouters.getResults().size());
+            assertEquals("Read a LogicalRouter different from the one just created and updated", logicalRouter, logicalRouters.get(0));
+            assertEquals("Read a LogicalRouter filtered by unique id (UUID) with more than one item", 1, logicalRouters.size());
 
-            assertEquals("", logicalRouters.getResults().get(0),
-                    api.findOneLogicalRouterByUuid(logicalRouter.getUuid()));
+            assertEquals(logicalRouters.get(0), api.findOneLogicalRouterByUuid(logicalRouter.getUuid()));
 
             // Before deleting the test LogicalRouter, test its ports
             final List<NiciraNvpTag> tags = new ArrayList<NiciraNvpTag>();
             tags.add(new NiciraNvpTag("cs_account", "OwnerName"));
 
             LogicalRouterPort logicalRouterPort = new LogicalRouterPort();
-            logicalRouterPort.setDisplayName("LRouterPort"+timestamp);
+            logicalRouterPort.setDisplayName("LRouterPort" + timestamp);
             logicalRouterPort.setTags(tags);
             logicalRouterPort.setAdminStatusEnabled(true);
             logicalRouterPort.setPortno(1024);
@@ -296,15 +270,13 @@ public class NiciraNvpApiIT {
             logicalRouterPort.setIpAddresses(ipAddresses);
             logicalRouterPort = api.createLogicalRouterPort(logicalRouter.getUuid(), logicalRouterPort);
 
-            logicalRouterPort.setDisplayName("UpdatedLRouterPort"+timestamp);
+            logicalRouterPort.setDisplayName("UpdatedLRouterPort" + timestamp);
             api.updateLogicalRouterPort(logicalRouter.getUuid(), logicalRouterPort);
 
-            final NiciraNvpList<LogicalRouterPort> logicalRouterePorts =
-                    api.findLogicalRouterPortsByUuid(logicalRouter.getUuid(), logicalRouterPort.getUuid());
-            for(final LogicalRouterPort iLRouterPort : logicalRouterePorts.getResults()) {
+            final List<LogicalRouterPort> logicalRouterePorts = api.findLogicalRouterPortsByUuid(logicalRouter.getUuid(), logicalRouterPort.getUuid());
+            for (final LogicalRouterPort iLRouterPort : logicalRouterePorts) {
                 if (iLRouterPort.getUuid().equalsIgnoreCase(logicalRouterPort.getUuid())) {
-                    assertEquals("Read a LogicalRouterPort different from the one just created and updated",
-                            logicalRouterPort, iLRouterPort);
+                    assertEquals("Read a LogicalRouterPort different from the one just created and updated", logicalRouterPort, iLRouterPort);
                 }
             }
 
@@ -339,8 +311,8 @@ public class NiciraNvpApiIT {
     public void testGetControlClusterStatus() throws NiciraNvpApiException {
         final ControlClusterStatus controlClusterStatus = api.getControlClusterStatus();
         final String clusterStatus = controlClusterStatus.getClusterStatus();
-        final boolean correctStatus = (clusterStatus.equalsIgnoreCase("stable") ||
-                clusterStatus.equalsIgnoreCase("joining") || clusterStatus.equalsIgnoreCase("unstable"));
+        final boolean correctStatus = clusterStatus.equalsIgnoreCase("stable") ||
+                        clusterStatus.equalsIgnoreCase("joining") || clusterStatus.equalsIgnoreCase("unstable");
         assertTrue("Not recognizable cluster status", correctStatus);
     }
 


Mime
View raw message