Skip to content

Commit c6602ee

Browse files
Refactor NSX api implementation (NiciraNvpApi)
- Make internal method private - Remove unused methods - Refactor type deserialization adapter classes out
1 parent 18e6b9b commit c6602ee

File tree

6 files changed

+229
-54
lines changed

6 files changed

+229
-54
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.network.nicira;
21+
22+
import java.lang.reflect.Type;
23+
24+
import com.google.gson.JsonDeserializationContext;
25+
import com.google.gson.JsonDeserializer;
26+
import com.google.gson.JsonElement;
27+
import com.google.gson.JsonObject;
28+
import com.google.gson.JsonParseException;
29+
30+
public class NatRuleAdapter implements JsonDeserializer<NatRule> {
31+
32+
@Override
33+
public NatRule deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
34+
final JsonObject jsonObject = jsonElement.getAsJsonObject();
35+
36+
if (!jsonObject.has("type")) {
37+
throw new JsonParseException("Deserializing as a NatRule, but no type present in the json object");
38+
}
39+
40+
final String natRuleType = jsonObject.get("type").getAsString();
41+
if ("SourceNatRule".equals(natRuleType)) {
42+
return context.deserialize(jsonElement, SourceNatRule.class);
43+
} else if ("DestinationNatRule".equals(natRuleType)) {
44+
return context.deserialize(jsonElement, DestinationNatRule.class);
45+
}
46+
47+
throw new JsonParseException("Failed to deserialize type \"" + natRuleType + "\"");
48+
}
49+
}

plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraNvpApi.java

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
import com.cloud.utils.rest.RESTValidationStrategy;
3333
import com.google.gson.JsonDeserializationContext;
3434
import com.google.gson.JsonDeserializer;
35-
import com.google.gson.JsonElement;
36-
import com.google.gson.JsonObject;
37-
import com.google.gson.JsonParseException;
3835
import com.google.gson.reflect.TypeToken;
3936

4037
@SuppressWarnings("rawtypes")
@@ -50,7 +47,7 @@ public class NiciraNvpApi {
5047
private static final String ROUTER_URI_PREFIX = "/ws.v1/lrouter";
5148
private static final String LOGIN_URL = "/ws.v1/login";
5249

53-
protected RESTServiceConnector restConnector;
50+
private final RESTServiceConnector restConnector;
5451

5552
protected final static Map<Class, String> prefixMap;
5653

@@ -111,7 +108,7 @@ public void setAdminCredentials(final String username, final String password) {
111108
* @return
112109
* @throws NiciraNvpApiException
113110
*/
114-
protected <T> T create(final T entity) throws NiciraNvpApiException {
111+
private <T> T create(final T entity) throws NiciraNvpApiException {
115112
final String uri = prefixMap.get(entity.getClass());
116113
return createWithUri(entity, uri);
117114
}
@@ -123,7 +120,7 @@ protected <T> T create(final T entity) throws NiciraNvpApiException {
123120
* @return
124121
* @throws NiciraNvpApiException
125122
*/
126-
protected <T> T createWithUri(final T entity, final String uri) throws NiciraNvpApiException {
123+
private <T> T createWithUri(final T entity, final String uri) throws NiciraNvpApiException {
127124
T createdEntity;
128125
try {
129126
createdEntity = restConnector.executeCreateObject(entity, new TypeToken<T>() {
@@ -135,27 +132,18 @@ protected <T> T createWithUri(final T entity, final String uri) throws NiciraNvp
135132
return createdEntity;
136133
}
137134

138-
/**
139-
* GET list of items
140-
*
141-
* @return
142-
* @throws NiciraNvpApiException
143-
*/
144-
protected <T> NiciraNvpList<T> find(final Class<T> clazz) throws NiciraNvpApiException {
145-
return find(null, clazz);
146-
}
147-
148135
/**
149136
* GET list of items
150137
*
151138
* @param uuid
139+
*
152140
* @return
153141
* @throws NiciraNvpApiException
154142
*/
155-
public <T> NiciraNvpList<T> find(final String uuid, final Class<T> clazz) throws NiciraNvpApiException {
143+
private <T> List<T> find(final Optional<String> uuid, final Class<T> clazz) throws NiciraNvpApiException {
156144
final String uri = prefixMap.get(clazz);
157145
Map<String, String> params = defaultListParams;
158-
if (uuid != null) {
146+
if (uuid.isPresent()) {
159147
params = new HashMap<String, String>(defaultListParams);
160148
params.put("uuid", uuid);
161149
}
@@ -181,7 +169,7 @@ public <T> NiciraNvpList<T> find(final String uuid, final Class<T> clazz) throws
181169
* @param uuid
182170
* @throws NiciraNvpApiException
183171
*/
184-
public <T> void update(final T item, final String uuid) throws NiciraNvpApiException {
172+
private <T> void update(final T item, final String uuid) throws NiciraNvpApiException {
185173
final String uri = prefixMap.get(item.getClass()) + "/" + uuid;
186174
updateWithUri(item, uri);
187175
}
@@ -193,7 +181,7 @@ public <T> void update(final T item, final String uuid) throws NiciraNvpApiExcep
193181
* @param uuid
194182
* @throws NiciraNvpApiException
195183
*/
196-
public <T> void updateWithUri(final T item, final String uri) throws NiciraNvpApiException {
184+
private <T> void updateWithUri(final T item, final String uri) throws NiciraNvpApiException {
197185
try {
198186
restConnector.executeUpdateObject(item, uri, Collections.<String, String> emptyMap());
199187
} catch (final CloudstackRESTException e) {
@@ -207,7 +195,7 @@ public <T> void updateWithUri(final T item, final String uri) throws NiciraNvpAp
207195
* @param securityProfileUuid
208196
* @throws NiciraNvpApiException
209197
*/
210-
public <T> void delete(final String uuid, final Class<T> clazz) throws NiciraNvpApiException {
198+
private <T> void delete(final String uuid, final Class<T> clazz) throws NiciraNvpApiException {
211199
final String uri = prefixMap.get(clazz) + "/" + uuid;
212200
deleteWithUri(uri);
213201
}
@@ -218,7 +206,7 @@ public <T> void delete(final String uuid, final Class<T> clazz) throws NiciraNvp
218206
* @param securityProfileUuid
219207
* @throws NiciraNvpApiException
220208
*/
221-
public void deleteWithUri(final String uri) throws NiciraNvpApiException {
209+
private void deleteWithUri(final String uri) throws NiciraNvpApiException {
222210
try {
223211
restConnector.executeDeleteObject(uri);
224212
} catch (final CloudstackRESTException e) {
@@ -588,48 +576,17 @@ public NiciraNvpList<LogicalRouterPort> findLogicalRouterPortByGatewayServiceUui
588576
}
589577
}
590578

591-
public static class NatRuleAdapter implements JsonDeserializer<NatRule> {
592579

593-
@Override
594-
public NatRule deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
595-
final JsonObject jsonObject = jsonElement.getAsJsonObject();
596580

597-
if (!jsonObject.has("type")) {
598-
throw new JsonParseException("Deserializing as a NatRule, but no type present in the json object");
599-
}
600581

601-
final String natRuleType = jsonObject.get("type").getAsString();
602-
if ("SourceNatRule".equals(natRuleType)) {
603-
return context.deserialize(jsonElement, SourceNatRule.class);
604-
} else if ("DestinationNatRule".equals(natRuleType)) {
605-
return context.deserialize(jsonElement, DestinationNatRule.class);
606-
}
607582

608-
throw new JsonParseException("Failed to deserialize type \"" + natRuleType + "\"");
609-
}
610583
}
611584

612-
public static class RoutingConfigAdapter implements JsonDeserializer<RoutingConfig> {
613585

614-
private static final String ROUTING_TABLE_ROUTING_CONFIG = "RoutingTableRoutingConfig";
615-
private static final String SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG = "SingleDefaultRouteImplicitRoutingConfig";
616586

617-
@Override
618-
public RoutingConfig deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
619-
final JsonObject jsonObject = jsonElement.getAsJsonObject();
620587

621-
if (!jsonObject.has("type")) {
622-
throw new JsonParseException("Deserializing as a RoutingConfig, but no type present in the json object");
623-
}
624588

625-
final String routingConfigType = jsonObject.get("type").getAsString();
626-
if (SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG.equals(routingConfigType)) {
627-
return context.deserialize(jsonElement, SingleDefaultRouteImplicitRoutingConfig.class);
628-
} else if (ROUTING_TABLE_ROUTING_CONFIG.equals(routingConfigType)) {
629-
return context.deserialize(jsonElement, RoutingTableRoutingConfig.class);
630-
}
631589

632-
throw new JsonParseException("Failed to deserialize type \"" + routingConfigType + "\"");
633590
}
634591
}
635592
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.network.nicira;
21+
22+
import java.lang.reflect.Type;
23+
24+
import com.google.gson.JsonDeserializationContext;
25+
import com.google.gson.JsonDeserializer;
26+
import com.google.gson.JsonElement;
27+
import com.google.gson.JsonObject;
28+
import com.google.gson.JsonParseException;
29+
30+
public class RoutingConfigAdapter implements JsonDeserializer<RoutingConfig> {
31+
32+
private static final String ROUTING_TABLE_ROUTING_CONFIG = "RoutingTableRoutingConfig";
33+
private static final String SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG = "SingleDefaultRouteImplicitRoutingConfig";
34+
35+
@Override
36+
public RoutingConfig deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) throws JsonParseException {
37+
final JsonObject jsonObject = jsonElement.getAsJsonObject();
38+
39+
if (!jsonObject.has("type")) {
40+
throw new JsonParseException("Deserializing as a RoutingConfig, but no type present in the json object");
41+
}
42+
43+
final String routingConfigType = jsonObject.get("type").getAsString();
44+
if (SINGLE_DEFAULT_ROUTE_IMPLICIT_ROUTING_CONFIG.equals(routingConfigType)) {
45+
return context.deserialize(jsonElement, SingleDefaultRouteImplicitRoutingConfig.class);
46+
} else if (ROUTING_TABLE_ROUTING_CONFIG.equals(routingConfigType)) {
47+
return context.deserialize(jsonElement, RoutingTableRoutingConfig.class);
48+
}
49+
50+
throw new JsonParseException("Failed to deserialize type \"" + routingConfigType + "\"");
51+
}
52+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.network.nicira;
21+
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.instanceOf;
24+
25+
import org.junit.Test;
26+
27+
import com.google.gson.Gson;
28+
import com.google.gson.GsonBuilder;
29+
import com.google.gson.JsonParseException;
30+
31+
public class NatRuleAdapterTest {
32+
private final Gson gson = new GsonBuilder()
33+
.registerTypeAdapter(NatRule.class, new NatRuleAdapter())
34+
.create();
35+
36+
@Test(expected = JsonParseException.class)
37+
public void testNatRuleAdapterNoType() {
38+
gson.fromJson("{}", NatRule.class);
39+
}
40+
41+
@Test(expected = JsonParseException.class)
42+
public void testNatRuleAdapterWrongType() {
43+
gson.fromJson("{type : \"WrongType\"}", NatRule.class);
44+
}
45+
46+
@Test()
47+
public void testNatRuleAdapterWithSourceNatRule() {
48+
final SourceNatRule sourceNatRule = (SourceNatRule) gson.fromJson("{type : \"SourceNatRule\"}", NatRule.class);
49+
50+
assertThat(sourceNatRule, instanceOf(SourceNatRule.class));
51+
}
52+
53+
@Test()
54+
public void testNatRuleAdapterWithDestinationNatRule() {
55+
final DestinationNatRule destinationNatRule = (DestinationNatRule) gson.fromJson("{type : \"DestinationNatRule\"}", NatRule.class);
56+
57+
assertThat(destinationNatRule, instanceOf(DestinationNatRule.class));
58+
}
59+
60+
}

plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NatRuleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class NatRuleTest {
3232
@Test
3333
public void testNatRuleEncoding() {
3434
final Gson gson =
35-
new GsonBuilder().registerTypeAdapter(NatRule.class, new NiciraNvpApi.NatRuleAdapter())
35+
new GsonBuilder().registerTypeAdapter(NatRule.class, new NatRuleAdapter())
3636
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
3737
.create();
3838

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.network.nicira;
21+
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.instanceOf;
24+
import static org.hamcrest.Matchers.notNullValue;
25+
26+
import org.junit.Test;
27+
28+
import com.google.gson.Gson;
29+
import com.google.gson.GsonBuilder;
30+
import com.google.gson.JsonParseException;
31+
32+
public class RoutingConfigAdapterTest {
33+
private final Gson gson = new GsonBuilder()
34+
.registerTypeAdapter(RoutingConfig.class, new RoutingConfigAdapter())
35+
.create();
36+
37+
@Test(expected = JsonParseException.class)
38+
public void testRoutingConfigAdapterNoType() {
39+
gson.fromJson("{}", RoutingConfig.class);
40+
}
41+
42+
@Test(expected = JsonParseException.class)
43+
public void testRoutingConfigAdapterWrongType() {
44+
gson.fromJson("{type : \"WrongType\"}", RoutingConfig.class);
45+
}
46+
47+
@Test()
48+
public void testRoutingConfigAdapter() throws Exception {
49+
final String jsonString = "{type : \"SingleDefaultRouteImplicitRoutingConfig\"}";
50+
51+
final SingleDefaultRouteImplicitRoutingConfig object = gson.fromJson(jsonString, SingleDefaultRouteImplicitRoutingConfig.class);
52+
53+
assertThat(object, notNullValue());
54+
assertThat(object, instanceOf(SingleDefaultRouteImplicitRoutingConfig.class));
55+
}
56+
57+
}

0 commit comments

Comments
 (0)