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 00000000000..7ea2f5b23bd --- /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 { + + @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 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 06446a7e7d6..39262b6916d 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 prefixMap; @@ -111,7 +108,7 @@ public class NiciraNvpApi { * @return * @throws NiciraNvpApiException */ - protected T create(final T entity) throws NiciraNvpApiException { + private 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 createWithUri(final T entity, final String uri) throws NiciraNvpApiException { + private T createWithUri(final T entity, final String uri) throws NiciraNvpApiException { T createdEntity; try { createdEntity = restConnector.executeCreateObject(entity, new TypeToken() { @@ -135,27 +132,18 @@ public class NiciraNvpApi { return createdEntity; } - /** - * GET list of items - * - * @return - * @throws NiciraNvpApiException - */ - protected NiciraNvpList find(final Class clazz) throws NiciraNvpApiException { - return find(null, clazz); - } - /** * GET list of items * * @param uuid + * * @return * @throws NiciraNvpApiException */ - public NiciraNvpList find(final String uuid, final Class clazz) throws NiciraNvpApiException { + private List find(final Optional uuid, final Class clazz) throws NiciraNvpApiException { final String uri = prefixMap.get(clazz); Map params = defaultListParams; - if (uuid != null) { + if (uuid.isPresent()) { params = new HashMap(defaultListParams); params.put("uuid", uuid); } @@ -181,7 +169,7 @@ public class NiciraNvpApi { * @param uuid * @throws NiciraNvpApiException */ - public void update(final T item, final String uuid) throws NiciraNvpApiException { + private 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 void updateWithUri(final T item, final String uri) throws NiciraNvpApiException { + private void updateWithUri(final T item, final String uri) throws NiciraNvpApiException { try { restConnector.executeUpdateObject(item, uri, Collections. emptyMap()); } catch (final CloudstackRESTException e) { @@ -207,7 +195,7 @@ public class NiciraNvpApi { * @param securityProfileUuid * @throws NiciraNvpApiException */ - public void delete(final String uuid, final Class clazz) throws NiciraNvpApiException { + private void delete(final String uuid, final Class 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 { - @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 { - 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 + "\""); } } } 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 00000000000..ad94f6343d8 --- /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 { + + 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 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 00000000000..199a6feedd7 --- /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)); + } + +} 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 d4c65969ec0..e4072bd82a9 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(); 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 00000000000..44269b23544 --- /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)); + } + +}