Multiple Sonar Fixes
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / impl / TenantCache.java
index 659202d..29b9cd3 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP : APPC
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
  * =============================================================================
@@ -18,7 +18,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  * ============LICENSE_END=========================================================
  */
 
@@ -28,8 +27,6 @@ import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 import org.onap.appc.Constants;
 import org.onap.appc.configuration.Configuration;
 import org.onap.appc.configuration.ConfigurationFactory;
@@ -45,9 +42,6 @@ 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.woorea.openstack.connector.JaxRs20Connector;
-// import com.sun.jersey.api.client.ClientHandlerException;
-import com.woorea.openstack.keystone.model.Access.Service.Endpoint;
 
 /**
  * This class maintains a cache of tenants within a specific provider.
@@ -62,8 +56,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
@@ -129,7 +121,6 @@ public class TenantCache implements Allocator<Context>, Destructor<Context> {
         configuration = ConfigurationFactory.getConfiguration();
         logger = EELFManager.getInstance().getLogger(getClass());
         this.provider = provider;
-        configuration = ConfigurationFactory.getConfiguration();
     }
 
     /**
@@ -165,12 +156,10 @@ public class TenantCache implements Allocator<Context>, Destructor<Context> {
         int max = configuration.getIntegerProperty(Constants.PROPERTY_MAX_POOL_SIZE);
         int delay = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_DELAY);
         int limit = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_LIMIT);
-
         String url = provider.getIdentityURL();
         String tenant = tenantName == null ? tenantId : tenantName;
         Properties properties = configuration.getProperties();
-        catalog = ServiceCatalogFactory.getServiceCatalog(url, tenant, userid, password, domain, properties);
-
+        catalog = getServiceCatalogFactory(url, tenant, properties);
         if (catalog == null) {
             logger.error(Msg.IAAS_UNSUPPORTED_IDENTITY_SERVICE, url);
             return;
@@ -182,48 +171,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;
             }
         }
@@ -233,6 +192,44 @@ public class TenantCache implements Allocator<Context>, Destructor<Context> {
         }
     }
 
+    public ServiceCatalog getServiceCatalogFactory(String url, String tenant, Properties properties) {
+        return ServiceCatalogFactory.getServiceCatalog(url, tenant, userid, password, domain, properties);
+    }
+
+    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, getTenantName());
+                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.
@@ -348,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;
     }
@@ -369,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);
         }
     }