3232import com .cloud .utils .rest .RESTValidationStrategy ;
3333import com .google .gson .JsonDeserializationContext ;
3434import com .google .gson .JsonDeserializer ;
35- import com .google .gson .JsonElement ;
36- import com .google .gson .JsonObject ;
37- import com .google .gson .JsonParseException ;
3835import 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}
0 commit comments