improved handling of indexed data
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / AAIRequest.java
index 21f8859..04312fd 100755 (executable)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017 AT&T Intellectual Property. All rights
  *             reserved.
  * ================================================================================
+ * Modifications Copyright (C) 2018 IBM.
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
-
+/**
+ * @author Rich Tabedzki
+ *
+ */
 package org.onap.ccsdk.sli.adaptors.aai;
 
 import java.io.IOException;
@@ -32,6 +37,7 @@ import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.BitSet;
@@ -45,7 +51,7 @@ import java.util.Set;
 import java.util.TreeSet;
 
 import org.apache.commons.lang.StringUtils;
-import org.openecomp.aai.inventory.v11.GenericVnf;
+import org.onap.aai.inventory.v14.GenericVnf;
 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -81,39 +87,26 @@ public abstract class AAIRequest {
     public static AAIRequest createRequest(String resoourceName, Map<String, String> nameValues){
 
         String resoource = resoourceName;
-               String masterResource = null;
+        String masterResource = null;
 
         if(resoource == null)
             return null;
 
         if(resoource.contains(":")) {
             String[] tokens = resoource.split(":");
-                       if(tokens != null && tokens.length == 2) {
-                               resoource = tokens[1];
-                               masterResource = tokens[0];
-                               //
-                               Class<? extends AAIDatum> clazz = null;
-                               try {
-                                       clazz = getClassFromResource(resoource) ;
-                               } catch (ClassNotFoundException e) {
-                                       LOG.warn("AAIRequest does not support class: " + e.getMessage());
-                                       return null;
-                               }
-
-                               if(clazz == null) {
-                                       return null;
-                               }
+            if(tokens != null && tokens.length == 2) {
+                resoource = tokens[1];
+                masterResource = tokens[0];
+                Class<? extends AAIDatum> clazz = getClassFromResource(resoource) ;
+
+                if(clazz == null) {
+                    return null;
+                }
             }
         }
 
         if(nameValues.containsKey("selflink")){
-            Class<? extends AAIDatum> clazz = null;
-            try {
-                clazz = getClassFromResource(resoource) ;
-            } catch (ClassNotFoundException e) {
-                LOG.warn("AAIRequest does not support class: " + e.getMessage());
-                return null;
-            }
+            Class<? extends AAIDatum> clazz = getClassFromResource(resoource) ;
 
             if(clazz != null)
                 return new SelfLinkRequest(clazz);
@@ -139,13 +132,13 @@ public abstract class AAIRequest {
         case "l2-bridge-sbg":
         case "l2-bridge-bgf":
             {
-               resoource = "l-interface";
+                resoource = "l-interface";
                 return getRequestFromResource("l-interface");
             }
-               case "relationship-list":
-                        return new RelationshipListRequest(AAIRequest.createRequest(masterResource, nameValues));
-               case "relationship":
-                        return new RelationshipRequest(AAIRequest.createRequest(masterResource, nameValues));
+        case "relationship-list":
+             return new RelationshipListRequest(AAIRequest.createRequest(masterResource, nameValues));
+        case "relationship":
+             return new RelationshipRequest(AAIRequest.createRequest(masterResource, nameValues));
         default:
                 return getRequestFromResource(resoource);
         }
@@ -186,7 +179,7 @@ public abstract class AAIRequest {
             }
 
             InputStream in = url.openStream();
-            Reader reader = new InputStreamReader(in, "UTF-8");
+            Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
 
             Properties properties = new Properties();
             properties.load(reader);
@@ -251,7 +244,7 @@ public abstract class AAIRequest {
         aaiService.getLogger().warn("Could not deserialize object of type " + lInterfaceRequest.getClass().getSimpleName(), exc) ;
     }
 
-       public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
+    public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
 
         String request_url = null;
 
@@ -278,7 +271,7 @@ public abstract class AAIRequest {
                     function = clazz.getMethod("processPathData", request_url.getClass(), requestProperties.getClass());
                     request_url = (String) function.invoke(null, request_url,  requestProperties);
                 } catch (Exception e) {
-                    e.printStackTrace();
+                       LOG.error("Caught exception", e);
                 }
             }
         }
@@ -295,16 +288,16 @@ public abstract class AAIRequest {
     }
 
     protected String getRequestPath(String resource) throws MalformedURLException {
-               if(requestProperties.containsKey("resource-path")) {
-                       return requestProperties.getProperty("resource-path");
-               }
+        if(requestProperties.containsKey("resource-path")) {
+            return requestProperties.getProperty("resource-path");
+        }
 
         Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
         if(resource != null) {
             // for group search add itself, but remove singular version of itself
             if(!uniqueResources.contains(resource)) {
                 boolean replaced =  false;
-                Set<String> tmpUniqueResources = new HashSet<String>();
+                Set<String> tmpUniqueResources = new HashSet<>();
                 tmpUniqueResources.addAll(uniqueResources);
                 for(String item : tmpUniqueResources){
                     String plural = item +"s";
@@ -344,7 +337,7 @@ public abstract class AAIRequest {
         return path;
     }
 
-       public abstract URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException;
+    public abstract URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException;
 
     public abstract String toJSONString();
 
@@ -419,7 +412,7 @@ public abstract class AAIRequest {
         return AAIService.getObjectMapper();
     }
 
-    public static Class<? extends AAIDatum> getClassFromResource(String resoourceName) throws ClassNotFoundException {
+    public static Class<? extends AAIDatum> getClassFromResource(String resoourceName) {
         String className = GenericVnf.class.getName();
         String[] split = resoourceName.split("-");
         for(int i = 0; i < split.length; i++) {
@@ -428,31 +421,22 @@ public abstract class AAIRequest {
 
         String caps = StringUtils.join(split);
         className = className.replace("GenericVnf", caps);
-        Class<? extends AAIDatum> clazz = null;
         try {
-            clazz = (Class<? extends AAIDatum>)Class.forName(className);
+            return (Class<? extends AAIDatum>)Class.forName(className);
         } catch (ClassNotFoundException e) {
             LOG.warn("AAIRequest does not support class: " + e.getMessage());
             return null;
         }
-
-        return clazz;
     }
 
     protected static AAIRequest getRequestFromResource(String resoourceName) {
 
-        Class<? extends AAIDatum> clazz = null;
-        try {
-            clazz = getClassFromResource(resoourceName);
-        } catch (ClassNotFoundException e) {
-            LOG.warn("AAIRequest does not support class: " + e.getMessage());
-            return null;
-        }
+        Class<? extends AAIDatum> clazz = getClassFromResource(resoourceName);
+
         if(clazz == null) {
             return null;
         }
-        GenericRequest request = new GenericRequest(clazz);
-        return request;
+        return new GenericRequest(clazz);
     }
 
     public static Map<String, String> splitQuery(String query) throws UnsupportedEncodingException {
@@ -489,4 +473,8 @@ public abstract class AAIRequest {
     public String getTargetUri() {
         return targetUri;
     }
+    public static final String getSupportedAAIVersion() {
+        return configProperties.getProperty(AAIDeclarations.AAI_VERSION, "/v14/");
+    }
 }