cloudstack-commits mailing list archives

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

- Make internal method private
- Remove unused methods
- Refactor type deserialization adapter classes out


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

Branch: refs/heads/master
Commit: c6602ee151968d266d22553e21f47bbc24de1199
Parents: 18e6b9b
Author: Miguel Ferreira <miguelferreira@me.com>
Authored: Sat Aug 22 16:12:12 2015 +0200
Committer: Miguel Ferreira <miguelferreira@me.com>
Committed: Tue Aug 25 17:36:16 2015 +0200

----------------------------------------------------------------------
 .../cloud/network/nicira/NatRuleAdapter.java    | 49 +++++++++++++++
 .../com/cloud/network/nicira/NiciraNvpApi.java  | 63 ++++----------------
 .../network/nicira/RoutingConfigAdapter.java    | 52 ++++++++++++++++
 .../network/nicira/NatRuleAdapterTest.java      | 60 +++++++++++++++++++
 .../com/cloud/network/nicira/NatRuleTest.java   |  2 +-
 .../nicira/RoutingConfigAdapterTest.java        | 57 ++++++++++++++++++
 6 files changed, 229 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c6602ee1/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NatRuleAdapter.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NatRuleAdapter.java
b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NatRuleAdapter.java
new file mode 100644
index 0000000..7ea2f5b
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NatRuleAdapter.java
@@ -0,0 +1,49 @@
+//
+// 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;
+
+import java.lang.reflect.Type;
+
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+
+public class NatRuleAdapter implements JsonDeserializer<NatRule> {
+
+    @Override
+    public NatRule deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext
context) throws JsonParseException {
+        final JsonObject jsonObject = jsonElement.getAsJsonObject();
+
+        if (!jsonObject.has("type")) {
+            throw new JsonParseException("Deserializing as a NatRule, but no type present
in the json object");
+        }
+
+        final String natRuleType = jsonObject.get("type").getAsString();
+        if ("SourceNatRule".equals(natRuleType)) {
+            return context.deserialize(jsonElement, SourceNatRule.class);
+        } else if ("DestinationNatRule".equals(natRuleType)) {
+            return context.deserialize(jsonElement, DestinationNatRule.class);
+        }
+
+        throw new JsonParseException("Failed to deserialize type \"" + natRuleType + "\"");
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c6602ee1/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 06446a7..39262b6 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
@@ -32,9 +32,6 @@ import com.cloud.utils.rest.RESTServiceConnector;
 import com.cloud.utils.rest.RESTValidationStrategy;
 import com.google.gson.JsonDeserializationContext;
 import com.google.gson.JsonDeserializer;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParseException;
 import com.google.gson.reflect.TypeToken;
 
 @SuppressWarnings("rawtypes")
@@ -50,7 +47,7 @@ public class NiciraNvpApi {
     private static final String ROUTER_URI_PREFIX = "/ws.v1/lrouter";
     private static final String LOGIN_URL = "/ws.v1/login";
 
-    protected RESTServiceConnector restConnector;
+    private final RESTServiceConnector restConnector;
 
     protected final static Map<Class, String> prefixMap;
 
@@ -111,7 +108,7 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    protected <T> T create(final T entity) throws NiciraNvpApiException {
+    private <T> T create(final T entity) throws NiciraNvpApiException {
         final String uri = prefixMap.get(entity.getClass());
         return createWithUri(entity, uri);
     }
@@ -123,7 +120,7 @@ public class NiciraNvpApi {
      * @return
      * @throws NiciraNvpApiException
      */
-    protected <T> T createWithUri(final T entity, final String uri) throws NiciraNvpApiException
{
+    private <T> T createWithUri(final T entity, final String uri) throws NiciraNvpApiException
{
         T createdEntity;
         try {
             createdEntity = restConnector.executeCreateObject(entity, new TypeToken<T>()
{
@@ -138,24 +135,15 @@ public class NiciraNvpApi {
     /**
      * GET list of items
      *
-     * @return
-     * @throws NiciraNvpApiException
-     */
-    protected <T> NiciraNvpList<T> find(final Class<T> clazz) throws NiciraNvpApiException
{
-        return find(null, clazz);
-    }
-
-    /**
-     * GET list of items
-     *
      * @param uuid
+     *
      * @return
      * @throws NiciraNvpApiException
      */
-    public <T> NiciraNvpList<T> find(final String uuid, final Class<T>
clazz) throws NiciraNvpApiException {
+    private <T> List<T> find(final Optional<String> uuid, final Class<T>
clazz) throws NiciraNvpApiException {
         final String uri = prefixMap.get(clazz);
         Map<String, String> params = defaultListParams;
-        if (uuid != null) {
+        if (uuid.isPresent()) {
             params = new HashMap<String, String>(defaultListParams);
             params.put("uuid", uuid);
         }
@@ -181,7 +169,7 @@ public class NiciraNvpApi {
      * @param uuid
      * @throws NiciraNvpApiException
      */
-    public <T> void update(final T item, final String uuid) throws NiciraNvpApiException
{
+    private <T> void update(final T item, final String uuid) throws NiciraNvpApiException
{
         final String uri = prefixMap.get(item.getClass()) + "/" + uuid;
         updateWithUri(item, uri);
     }
@@ -193,7 +181,7 @@ public class NiciraNvpApi {
      * @param uuid
      * @throws NiciraNvpApiException
      */
-    public <T> void updateWithUri(final T item, final String uri) throws NiciraNvpApiException
{
+    private <T> void updateWithUri(final T item, final String uri) throws NiciraNvpApiException
{
         try {
             restConnector.executeUpdateObject(item, uri, Collections.<String, String>
emptyMap());
         } catch (final CloudstackRESTException e) {
@@ -207,7 +195,7 @@ public class NiciraNvpApi {
      * @param securityProfileUuid
      * @throws NiciraNvpApiException
      */
-    public <T> void delete(final String uuid, final Class<T> clazz) throws NiciraNvpApiException
{
+    private <T> void delete(final String uuid, final Class<T> clazz) throws NiciraNvpApiException
{
         final String uri = prefixMap.get(clazz) + "/" + uuid;
         deleteWithUri(uri);
     }
@@ -218,7 +206,7 @@ public class NiciraNvpApi {
      * @param securityProfileUuid
      * @throws NiciraNvpApiException
      */
-    public void deleteWithUri(final String uri) throws NiciraNvpApiException {
+    private void deleteWithUri(final String uri) throws NiciraNvpApiException {
         try {
             restConnector.executeDeleteObject(uri);
         } catch (final CloudstackRESTException e) {
@@ -588,48 +576,17 @@ public class NiciraNvpApi {
         }
     }
 
-    public static class NatRuleAdapter implements JsonDeserializer<NatRule> {
 
-        @Override
-        public NatRule deserialize(final JsonElement jsonElement, final Type type, final
JsonDeserializationContext context) throws JsonParseException {
-            final JsonObject jsonObject = jsonElement.getAsJsonObject();
 
-            if (!jsonObject.has("type")) {
-                throw new JsonParseException("Deserializing as a NatRule, but no type present
in the json object");
-            }
 
-            final String natRuleType = jsonObject.get("type").getAsString();
-            if ("SourceNatRule".equals(natRuleType)) {
-                return context.deserialize(jsonElement, SourceNatRule.class);
-            } else if ("DestinationNatRule".equals(natRuleType)) {
-                return context.deserialize(jsonElement, DestinationNatRule.class);
-            }
 
-            throw new JsonParseException("Failed to deserialize type \"" + natRuleType +
"\"");
-        }
     }
 
-    public static class RoutingConfigAdapter implements JsonDeserializer<RoutingConfig>
{
 
-        private static final String ROUTING_TABLE_ROUTING_CONFIG = "RoutingTableRoutingConfig";
-        private static final String SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG = "SingleDefaultRouteImplicitRoutingConfig";
 
-        @Override
-        public RoutingConfig deserialize(final JsonElement jsonElement, final Type type,
final JsonDeserializationContext context) throws JsonParseException {
-            final JsonObject jsonObject = jsonElement.getAsJsonObject();
 
-            if (!jsonObject.has("type")) {
-                throw new JsonParseException("Deserializing as a RoutingConfig, but no type
present in the json object");
-            }
 
-            final String routingConfigType = jsonObject.get("type").getAsString();
-            if (SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG.equals(routingConfigType)) {
-                return context.deserialize(jsonElement, SingleDefaultRouteImplicitRoutingConfig.class);
-            } else if (ROUTING_TABLE_ROUTING_CONFIG.equals(routingConfigType)) {
-                return context.deserialize(jsonElement, RoutingTableRoutingConfig.class);
-            }
 
-            throw new JsonParseException("Failed to deserialize type \"" + routingConfigType
+ "\"");
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c6602ee1/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/RoutingConfigAdapter.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/RoutingConfigAdapter.java
b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/RoutingConfigAdapter.java
new file mode 100644
index 0000000..ad94f63
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/RoutingConfigAdapter.java
@@ -0,0 +1,52 @@
+//
+// 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;
+
+import java.lang.reflect.Type;
+
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+
+public class RoutingConfigAdapter implements JsonDeserializer<RoutingConfig> {
+
+    private static final String ROUTING_TABLE_ROUTING_CONFIG = "RoutingTableRoutingConfig";
+    private static final String SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG = "SingleDefaultRouteImplicitRoutingConfig";
+
+    @Override
+    public RoutingConfig deserialize(final JsonElement jsonElement, final Type type, final
JsonDeserializationContext context) throws JsonParseException {
+        final JsonObject jsonObject = jsonElement.getAsJsonObject();
+
+        if (!jsonObject.has("type")) {
+            throw new JsonParseException("Deserializing as a RoutingConfig, but no type present
in the json object");
+        }
+
+        final String routingConfigType = jsonObject.get("type").getAsString();
+        if (SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG.equals(routingConfigType)) {
+            return context.deserialize(jsonElement, SingleDefaultRouteImplicitRoutingConfig.class);
+        } else if (ROUTING_TABLE_ROUTING_CONFIG.equals(routingConfigType)) {
+            return context.deserialize(jsonElement, RoutingTableRoutingConfig.class);
+        }
+
+        throw new JsonParseException("Failed to deserialize type \"" + routingConfigType
+ "\"");
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c6602ee1/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleAdapterTest.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleAdapterTest.java
b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleAdapterTest.java
new file mode 100644
index 0000000..199a6fe
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleAdapterTest.java
@@ -0,0 +1,60 @@
+//
+// 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;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.instanceOf;
+
+import org.junit.Test;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParseException;
+
+public class NatRuleAdapterTest {
+    private final Gson gson = new GsonBuilder()
+        .registerTypeAdapter(NatRule.class, new NatRuleAdapter())
+        .create();
+
+    @Test(expected = JsonParseException.class)
+    public void testNatRuleAdapterNoType() {
+        gson.fromJson("{}", NatRule.class);
+    }
+
+    @Test(expected = JsonParseException.class)
+    public void testNatRuleAdapterWrongType() {
+        gson.fromJson("{type : \"WrongType\"}", NatRule.class);
+    }
+
+    @Test()
+    public void testNatRuleAdapterWithSourceNatRule() {
+        final SourceNatRule sourceNatRule = (SourceNatRule) gson.fromJson("{type : \"SourceNatRule\"}",
NatRule.class);
+
+        assertThat(sourceNatRule, instanceOf(SourceNatRule.class));
+    }
+
+    @Test()
+    public void testNatRuleAdapterWithDestinationNatRule() {
+        final DestinationNatRule destinationNatRule = (DestinationNatRule) gson.fromJson("{type
: \"DestinationNatRule\"}", NatRule.class);
+
+        assertThat(destinationNatRule, instanceOf(DestinationNatRule.class));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c6602ee1/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleTest.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleTest.java
b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleTest.java
index d4c6596..e4072bd 100644
--- a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleTest.java
+++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleTest.java
@@ -32,7 +32,7 @@ public class NatRuleTest {
     @Test
     public void testNatRuleEncoding() {
         final Gson gson =
-            new GsonBuilder().registerTypeAdapter(NatRule.class, new NiciraNvpApi.NatRuleAdapter())
+            new GsonBuilder().registerTypeAdapter(NatRule.class, new NatRuleAdapter())
                 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                 .create();
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c6602ee1/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/RoutingConfigAdapterTest.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/RoutingConfigAdapterTest.java
b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/RoutingConfigAdapterTest.java
new file mode 100644
index 0000000..44269b2
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/RoutingConfigAdapterTest.java
@@ -0,0 +1,57 @@
+//
+// 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;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.notNullValue;
+
+import org.junit.Test;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParseException;
+
+public class RoutingConfigAdapterTest {
+    private final Gson gson = new GsonBuilder()
+        .registerTypeAdapter(RoutingConfig.class, new RoutingConfigAdapter())
+        .create();
+
+    @Test(expected = JsonParseException.class)
+    public void testRoutingConfigAdapterNoType() {
+        gson.fromJson("{}", RoutingConfig.class);
+    }
+
+    @Test(expected = JsonParseException.class)
+    public void testRoutingConfigAdapterWrongType() {
+        gson.fromJson("{type : \"WrongType\"}", RoutingConfig.class);
+    }
+
+    @Test()
+    public void testRoutingConfigAdapter() throws Exception {
+        final String jsonString = "{type : \"SingleDefaultRouteImplicitRoutingConfig\"}";
+
+        final SingleDefaultRouteImplicitRoutingConfig object = gson.fromJson(jsonString,
SingleDefaultRouteImplicitRoutingConfig.class);
+
+        assertThat(object, notNullValue());
+        assertThat(object, instanceOf(SingleDefaultRouteImplicitRoutingConfig.class));
+    }
+
+}


Mime
View raw message