Refactor NSX api implementation (NiciraNvpApi)

- Make internal method private
- Remove unused methods
- Refactor type deserialization adapter classes out
This commit is contained in:
Miguel Ferreira 2015-08-22 16:12:12 +02:00
parent 18e6b9ba78
commit c6602ee151
6 changed files with 229 additions and 54 deletions

View File

@ -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 + "\"");
}
}

View File

@ -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>() {
@ -135,27 +132,18 @@ public class NiciraNvpApi {
return createdEntity;
}
/**
* 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 + "\"");
}
}
}

View File

@ -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 + "\"");
}
}

View File

@ -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));
}
}

View File

@ -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();

View File

@ -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));
}
}