ServiceCatalogV3 fixes 13/29513/10
authorJakub Dudycz <jakub.dudycz@nokia.com>
Tue, 30 Jan 2018 16:50:46 +0000 (17:50 +0100)
committerPatrick Brady <pb071s@att.com>
Wed, 7 Feb 2018 20:09:21 +0000 (20:09 +0000)
Change-Id: I28d22a5d6206b4e4aaed3a017b4db39d0d5f0709
Issue-ID: APPC-549
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/ServiceCatalogV3.java

index bd41c3f..2fea0b1 100644 (file)
@@ -9,15 +9,15 @@
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
- * \r
+ *\r
  *      http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
+ *\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
- * \r
+ *\r
  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
  * ============LICENSE_END=========================================================\r
  */\r
@@ -30,6 +30,7 @@ import com.att.cdp.openstack.util.ExceptionMapper;
 import com.att.cdp.pal.util.Time;\r
 import com.att.cdp.zones.ContextFactory;\r
 import com.att.cdp.zones.spi.RequestState;\r
+import com.google.common.collect.Lists;\r
 import com.woorea.openstack.base.client.OpenStackBaseException;\r
 import com.woorea.openstack.base.client.OpenStackClientConnector;\r
 import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider;\r
@@ -113,7 +114,7 @@ public class ServiceCatalogV3 extends ServiceCatalog {
      * {@inheritDoc}\r
      */\r
     public ServiceCatalogV3(String identityURL, String projectIdentifier, String principal, String credential,\r
-            String domain, Properties properties) {\r
+        String domain, Properties properties) {\r
         super(identityURL, projectIdentifier, principal, credential, domain, properties);\r
     }\r
 \r
@@ -132,7 +133,7 @@ public class ServiceCatalogV3 extends ServiceCatalog {
             connectorClass = Class.forName(CLIENT_CONNECTOR_CLASS);\r
             connector = (OpenStackClientConnector) connectorClass.newInstance();\r
         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {\r
-            e.printStackTrace();\r
+            logger.error("An error occurred when initializing ServiceCatalogV3", e);\r
             return;\r
         }\r
         Keystone keystone = new Keystone(identityURL, connector);\r
@@ -146,21 +147,14 @@ public class ServiceCatalogV3 extends ServiceCatalog {
         }\r
         if (trustedHosts != null) {\r
             keystone.getProperties().setProperty(com.woorea.openstack.common.client.Constants.TRUST_HOST_LIST,\r
-                    trustedHosts);\r
+                trustedHosts);\r
         }\r
 \r
         // create identity\r
         Identity identity = Identity.password(domain, principal, credential);\r
 \r
         // create scope\r
-        Scope scope = null;\r
-        if (projectIdentifier.length() == 32 && projectIdentifier.matches("[0-9a-fA-F]+")) { //$NON-NLS-1$\r
-            // authenticate = authenticate.withTenantId(projectIdentifier);\r
-            scope = Scope.project(projectIdentifier);\r
-        } else {\r
-            // authenticate = authenticate.withTenantName(projectIdentifier);\r
-            scope = Scope.project(domain, projectIdentifier);\r
-        }\r
+        Scope scope = initScope();\r
 \r
         Authentication authentication = new Authentication();\r
         authentication.setIdentity(identity);\r
@@ -186,8 +180,16 @@ public class ServiceCatalogV3 extends ServiceCatalog {
             parseServiceCatalog(token.getCatalog());\r
         } catch (OpenStackBaseException e) {\r
             ExceptionMapper.mapException(e);\r
-        } catch (Exception ex) {\r
-            throw new ContextConnectionException(ex.getMessage());\r
+        } catch (Exception e) {\r
+            throw new ContextConnectionException(e);\r
+        }\r
+    }\r
+\r
+    private Scope initScope() {\r
+        if (projectIdentifier.length() == 32 && projectIdentifier.matches("[0-9a-fA-F]+")) { //$NON-NLS-1$\r
+            return Scope.project(projectIdentifier);\r
+        } else {\r
+            return Scope.project(domain, projectIdentifier);\r
         }\r
     }\r
 \r
@@ -275,23 +277,21 @@ public class ServiceCatalogV3 extends ServiceCatalog {
             for (Endpoint endpoint : getEndpoints(ServiceCatalog.COMPUTE_SERVICE)) {\r
                 String endpointUrl = endpoint.getUrl();\r
                 Matcher matcher = urlPattern.matcher(endpointUrl);\r
-                if (matcher.matches()) {\r
-                    if (url.getHost().equals(matcher.group(1))) {\r
-                        if (url.getPort() != null) {\r
-                            if (!url.getPort().equals(matcher.group(2))) {\r
-                                continue;\r
-                            }\r
-                        }\r
-\r
-                        region = endpoint.getRegion();\r
-                        break;\r
-                    }\r
+                if (validateUrl(url, matcher)) {\r
+                    region = endpoint.getRegion();\r
+                    break;\r
                 }\r
             }\r
         }\r
         return region;\r
     }\r
 \r
+    private boolean validateUrl(VMURL url, Matcher matcher) {\r
+        return matcher.matches()\r
+            && url.getHost().equals(matcher.group(1))\r
+            && (url.getPort() == null || url.getPort().equals(matcher.group(2)));\r
+    }\r
+\r
     /**\r
      * {@inheritDoc}\r
      */\r
@@ -317,22 +317,28 @@ public class ServiceCatalogV3 extends ServiceCatalog {
         lock.lock();\r
         try {\r
             builder.append(String.format("Service Catalog: tenant %s, id[%s]%n", project.getName(), //$NON-NLS-1$\r
-                    project.getId()));\r
+                project.getId()));\r
             if (regions != null && !regions.isEmpty()) {\r
                 builder.append(String.format("%d regions:%n", regions.size())); //$NON-NLS-1$\r
                 for (String region : regions) {\r
-                    builder.append("\t" + region + "%n"); //$NON-NLS-1$ //$NON-NLS-2$\r
+                    //$NON-NLS-1$ //$NON-NLS-2$\r
+                    builder\r
+                        .append("\t")\r
+                        .append(region)\r
+                        .append("%n");\r
                 }\r
             }\r
             builder.append(String.format("%d services:%n", serviceEndpoints.size())); //$NON-NLS-1$\r
-            for (String serviceType : serviceEndpoints.keySet()) {\r
-                List<Service.Endpoint> endpoints = serviceEndpoints.get(serviceType);\r
-                Service service = serviceTypes.get(serviceType);\r
+\r
+            for (Map.Entry<String, List<Service.Endpoint>> entry : serviceEndpoints.entrySet()) {\r
+                Service service = serviceTypes.get(entry.getKey());\r
 \r
                 builder.append(String.format("\t%s - %d endpoints%n", service.getType(), //$NON-NLS-1$\r
-                        endpoints.size()));\r
-                for (Service.Endpoint endpoint : endpoints) {\r
-                    builder.append(String.format("\t\tRegion [%s], public URL [%s]%n", endpoint.getRegion(), //$NON-NLS-1$\r
+                    entry.getValue().size()));\r
+\r
+                for (Service.Endpoint endpoint : entry.getValue()) {\r
+                    builder\r
+                        .append(String.format("\t\tRegion [%s], public URL [%s]%n", endpoint.getRegion(), //$NON-NLS-1$\r
                             endpoint.getUrl()));\r
                 }\r
             }\r
@@ -345,7 +351,7 @@ public class ServiceCatalogV3 extends ServiceCatalog {
 \r
     /**\r
      * Parses the service catalog and caches the results\r
-     * \r
+     *\r
      * @param services The list of services published by this provider\r
      */\r
     private void parseServiceCatalog(List<Service> services) {\r
@@ -359,31 +365,30 @@ public class ServiceCatalogV3 extends ServiceCatalog {
             for (Service service : services) {\r
                 String type = service.getType();\r
                 serviceTypes.put(type, service);\r
-\r
-                List<Service.Endpoint> endpoints = service.getEndpoints();\r
-                for (Service.Endpoint endpoint : endpoints) {\r
-                    List<Service.Endpoint> endpointList = serviceEndpoints.get(type);\r
-                    if (endpointList == null) {\r
-                        endpointList = new ArrayList<>();\r
-                        serviceEndpoints.put(type, endpointList);\r
-                    }\r
-                    endpointList.add(endpoint);\r
-\r
-                    String region = endpoint.getRegion();\r
-                    if (!regions.contains(region)) {\r
-                        regions.add(region);\r
-                    }\r
-                }\r
+                addRegions(service, type);\r
             }\r
         } finally {\r
             lock.unlock();\r
         }\r
     }\r
 \r
+    private void addRegions(Service service, String type) {\r
+        List<Endpoint> endpoints = service.getEndpoints();\r
+        for (Endpoint endpoint : endpoints) {\r
+            serviceEndpoints.computeIfAbsent(type, val -> new ArrayList<>());\r
+            serviceEndpoints.get(type).add(endpoint);\r
+\r
+            String region = endpoint.getRegion();\r
+            if (!regions.contains(region)) {\r
+                regions.add(region);\r
+            }\r
+        }\r
+    }\r
+\r
     /**\r
      * Computes the local time when the access token will expire, after which we will need to re-login to access the\r
      * provider.\r
-     * \r
+     *\r
      * @param accessKey The access key used to access the provider\r
      * @return The local time the key expires\r
      */\r
@@ -399,4 +404,4 @@ public class ServiceCatalogV3 extends ServiceCatalog {
         }\r
         return now.getTime();\r
     }\r
-}\r
+}
\ No newline at end of file