TenantCache sonar fixes 09/30509/2
authorJakub Dudycz <jakub.dudycz@nokia.com>
Tue, 6 Feb 2018 17:11:49 +0000 (18:11 +0100)
committerPatrick Brady <pb071s@att.com>
Wed, 7 Feb 2018 19:30:38 +0000 (19:30 +0000)
Change-Id: I8c95c1913edb325faac49f416d16f42683d8fa7e
Issue-ID: APPC-574
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/impl/TenantCache.java

index 3f59503..ac244b1 100644 (file)
 
 package org.onap.appc.adapter.iaas.impl;
 
+import com.att.cdp.exceptions.ContextConnectionException;
+import com.att.cdp.exceptions.ZoneException;
+import com.att.cdp.zones.Context;
+import com.att.cdp.zones.ContextFactory;
+import com.att.cdp.zones.Provider;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
@@ -36,14 +43,6 @@ import org.onap.appc.pool.Allocator;
 import org.onap.appc.pool.Destructor;
 import org.onap.appc.pool.Pool;
 import org.onap.appc.pool.PoolSpecificationException;
-import com.att.cdp.exceptions.ContextConnectionException;
-import com.att.cdp.exceptions.ZoneException;
-import com.att.cdp.zones.Context;
-import com.att.cdp.zones.ContextFactory;
-import com.att.cdp.zones.Provider;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-// import com.sun.jersey.api.client.ClientHandlerException;
 
 /**
  * This class maintains a cache of tenants within a specific provider.
@@ -58,8 +57,6 @@ public class TenantCache implements Allocator<Context>, Destructor<Context> {
 
     public static final String POOL_PROVIDER_NAME = "pool.provider.name";
     public static final String POOL_TENANT_NAME = "pool.tenant.name";
-    // public static final String CLIENT_CONNECTOR_CLASS =
-    // "com.woorea.openstack.connector.JerseyConnector";
     public static final String CLIENT_CONNECTOR_CLASS = "com.woorea.openstack.connector.JaxRs20Connector";
     /**
      * The domain to use to authenticate
@@ -178,48 +175,18 @@ public class TenantCache implements Allocator<Context>, Destructor<Context> {
                 catalog.init();
                 tenantId = catalog.getProjectId();
                 tenantName = catalog.getProjectName();
-
-                for (String region : catalog.getRegions()) {
-                    try {
-                        Pool<Context> pool = new Pool<>(min, max);
-                        pool.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, url);
-                        pool.setProperty(ContextFactory.PROPERTY_TENANT, tenantName);
-                        pool.setProperty(ContextFactory.PROPERTY_CLIENT_CONNECTOR_CLASS, CLIENT_CONNECTOR_CLASS);
-                        pool.setProperty(ContextFactory.PROPERTY_RETRY_DELAY,
-                                configuration.getProperty(Constants.PROPERTY_RETRY_DELAY));
-                        pool.setProperty(ContextFactory.PROPERTY_RETRY_LIMIT,
-                                configuration.getProperty(Constants.PROPERTY_RETRY_LIMIT));
-                        pool.setProperty(ContextFactory.PROPERTY_REGION, region);
-                        if (properties.getProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS) != null) {
-                            pool.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS,
-                                    properties.getProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS));
-                        }
-                        pool.setAllocator(this);
-                        pool.setDestructor(this);
-                        pools.put(region, pool);
-                        logger.debug(String.format("Put pool for region %s", region));
-                    } catch (PoolSpecificationException e) {
-                        logger.error("Error creating pool", e);
-                        e.printStackTrace();
-                    }
-                }
+                createPools(min, max, url, properties);
                 initialized = true;
                 break;
             } catch (ContextConnectionException e) {
-                attempt++;
-                if (attempt <= limit) {
+                if (++attempt <= limit) {
                     logger.error(Msg.CONNECTION_FAILED_RETRY, provider.getProviderName(), url, tenantName, tenantId,
                             e.getMessage(), Integer.toString(delay), Integer.toString(attempt),
                             Integer.toString(limit));
-
-                    try {
-                        Thread.sleep(delay * 1000L);
-                    } catch (InterruptedException ie) {
-                        // ignore
-                    }
+                    sleep(delay);
                 }
             } catch (ZoneException e) {
-                logger.error(e.getMessage());
+                logger.error("An error occurred when initializing cache", e);
                 break;
             }
         }
@@ -229,6 +196,40 @@ public class TenantCache implements Allocator<Context>, Destructor<Context> {
         }
     }
 
+    private void createPools(int min, int max, String url, Properties properties) {
+        for (String region : catalog.getRegions()) {
+            try {
+                Pool<Context> pool = new Pool<>(min, max);
+                pool.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, url);
+                pool.setProperty(ContextFactory.PROPERTY_TENANT, tenantName);
+                pool.setProperty(ContextFactory.PROPERTY_CLIENT_CONNECTOR_CLASS, CLIENT_CONNECTOR_CLASS);
+                pool.setProperty(ContextFactory.PROPERTY_RETRY_DELAY,
+                        configuration.getProperty(Constants.PROPERTY_RETRY_DELAY));
+                pool.setProperty(ContextFactory.PROPERTY_RETRY_LIMIT,
+                        configuration.getProperty(Constants.PROPERTY_RETRY_LIMIT));
+                pool.setProperty(ContextFactory.PROPERTY_REGION, region);
+                if (properties.getProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS) != null) {
+                    pool.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS,
+                            properties.getProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS));
+                }
+                pool.setAllocator(this);
+                pool.setDestructor(this);
+                pools.put(region, pool);
+                logger.debug(String.format("Put pool for region %s", region));
+            } catch (PoolSpecificationException e) {
+                logger.error("Error creating pool", e);
+            }
+        }
+    }
+
+    private void sleep(int delay) {
+        try {
+            Thread.sleep(delay * 1000L);
+        } catch (InterruptedException ie) {
+            // ignore
+        }
+    }
+
     /**
      * This method accepts a fully qualified compute node URL and uses that to determine which region of the provider
      * hosts that compute node.
@@ -344,15 +345,11 @@ public class TenantCache implements Allocator<Context>, Destructor<Context> {
         Class<? extends Provider> providerClass;
         try {
             providerClass = (Class<? extends Provider>) Class.forName("com.att.cdp.openstack.OpenStackProvider");
-            // String providerType = provider.getProviderType();
-
-            // Context context = ContextFactory.getContext(providerType, pool.getProperties());
             Context context = ContextFactory.getContext(providerClass, pool.getProperties());
             context.login(userid, password);
             return context;
         } catch (IllegalStateException | IllegalArgumentException | ZoneException | ClassNotFoundException e) {
             logger.debug("Failed to allocate context for pool", e);
-            e.printStackTrace();
         }
         return null;
     }
@@ -365,7 +362,7 @@ public class TenantCache implements Allocator<Context>, Destructor<Context> {
         try {
             context.close();
         } catch (IOException e) {
-            e.printStackTrace();
+            logger.error("An error occurred when destroying cache", e);
         }
     }