[SDNC-30] summary
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / AAIService.java
@@ -36,6 +36,7 @@ import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
+import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLEncoder;
@@ -66,6 +67,7 @@ import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSession;
 import javax.net.ssl.SSLSocketFactory;
 import javax.ws.rs.HttpMethod;
+import javax.ws.rs.core.UriBuilder;
 import javax.xml.bind.annotation.XmlElement;
 
 import org.apache.commons.codec.binary.Base64;
@@ -75,29 +77,29 @@ import org.onap.ccsdk.sli.core.sli.MetricLogger;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
-import org.openecomp.aai.inventory.v10.AvailabilityZone;
-import org.openecomp.aai.inventory.v10.Complex;
-import org.openecomp.aai.inventory.v10.CtagPool;
-import org.openecomp.aai.inventory.v10.DvsSwitch;
-import org.openecomp.aai.inventory.v10.GenericVnf;
-import org.openecomp.aai.inventory.v10.L3Network;
-import org.openecomp.aai.inventory.v10.OamNetwork;
-import org.openecomp.aai.inventory.v10.PInterface;
-import org.openecomp.aai.inventory.v10.PhysicalLink;
-import org.openecomp.aai.inventory.v10.Pserver;
-import org.openecomp.aai.inventory.v10.ResultData;
-import org.openecomp.aai.inventory.v10.SearchResults;
-import org.openecomp.aai.inventory.v10.Service;
-import org.openecomp.aai.inventory.v10.ServiceInstance;
-import org.openecomp.aai.inventory.v10.SitePairSet;
-import org.openecomp.aai.inventory.v10.Tenant;
-import org.openecomp.aai.inventory.v10.Vce;
-import org.openecomp.aai.inventory.v10.VnfImage;
-import org.openecomp.aai.inventory.v10.VnfImages;
-import org.openecomp.aai.inventory.v10.Vpe;
-import org.openecomp.aai.inventory.v10.VplsPe;
-import org.openecomp.aai.inventory.v10.VpnBinding;
-import org.openecomp.aai.inventory.v10.Vserver;
+import org.openecomp.aai.inventory.v11.AvailabilityZone;
+import org.openecomp.aai.inventory.v11.Complex;
+import org.openecomp.aai.inventory.v11.CtagPool;
+import org.openecomp.aai.inventory.v11.DvsSwitch;
+import org.openecomp.aai.inventory.v11.GenericVnf;
+import org.openecomp.aai.inventory.v11.L3Network;
+import org.openecomp.aai.inventory.v11.OamNetwork;
+import org.openecomp.aai.inventory.v11.PInterface;
+import org.openecomp.aai.inventory.v11.PhysicalLink;
+import org.openecomp.aai.inventory.v11.Pserver;
+import org.openecomp.aai.inventory.v11.ResultData;
+import org.openecomp.aai.inventory.v11.SearchResults;
+import org.openecomp.aai.inventory.v11.Service;
+import org.openecomp.aai.inventory.v11.ServiceInstance;
+import org.openecomp.aai.inventory.v11.SitePairSet;
+import org.openecomp.aai.inventory.v11.Tenant;
+import org.openecomp.aai.inventory.v11.Vce;
+import org.openecomp.aai.inventory.v11.VnfImage;
+import org.openecomp.aai.inventory.v11.VnfImages;
+import org.openecomp.aai.inventory.v11.Vpe;
+import org.openecomp.aai.inventory.v11.VplsPe;
+import org.openecomp.aai.inventory.v11.VpnBinding;
+import org.openecomp.aai.inventory.v11.Vserver;
 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
 import org.onap.ccsdk.sli.adaptors.aai.data.ErrorResponse;
 import org.onap.ccsdk.sli.adaptors.aai.data.RequestError;
@@ -383,7 +385,13 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
        con.setRequestProperty( "Content-Type",  "PATCH".equalsIgnoreCase(method) ? "application/merge-patch+json" : "application/json" );
         con.setRequestProperty("X-FromAppId", application_id);
         con.setRequestProperty("X-TransactionId",TransactionIdTracker.getNextTransactionId());
-        con.setRequestProperty(MetricLogger.REQUEST_ID, ml.getRequestID());
+        String mlId = ml.getRequestID();
+        if(mlId != null && !mlId.isEmpty()) {
+               LOG.debug(String.format("MetricLogger requestId = %s", mlId));
+               con.setRequestProperty(MetricLogger.REQUEST_ID, mlId);
+        } else {
+               LOG.debug("MetricLogger requestId is null");
+        }
 
         if(user_name != null && !user_name.isEmpty() && user_password != null && !user_password.isEmpty()) {
                String basicAuth = "Basic " + new String(Base64.encodeBase64((user_name + ":" + user_password).getBytes()));
@@ -697,7 +705,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
        {
                String encrypted_url = selection;
                String apnpattern =
-                               "/aai/v10/business/customers/customer/(.+)/service-subscriptions/service-subscription/(.+)/service-instances/service-instance/(.+)/";
+                               "/aai/v11/business/customers/customer/(.+)/service-subscriptions/service-subscription/(.+)/service-instances/service-instance/(.+)/";
                Pattern pattern = Pattern.compile(apnpattern);
 
                try {
@@ -1773,8 +1781,12 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
         SvcLogicContext ctx = new SvcLogicContext();
         if(!key.contains(" = ") && isValidURL(key)) {
                key = String.format("selflink = '%s'", key);
+        } else
+        if(!key.contains(" = ") && isValidURI(key)) {
+               key = String.format("resource-path = '%s'", key);
         }
-        HashMap<String, String> nameValues = keyToHashMap(key, ctx);
+
+        HashMap<String, String> nameValues = AAIServiceUtils.keyToHashMap(key, ctx);
 
         SelfLinkRequest request = new SelfLinkRequest(type);
         request.processRequestPathValues(nameValues);
@@ -1974,12 +1986,12 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
 
 
        @Override
-       public VnfImage requestVnfImageData(String att_uuid) throws AAIServiceException {
+       public VnfImage requestVnfImageData(String vnf_image_uuid) throws AAIServiceException {
                VnfImage response = null;
 
                try {
                        AAIRequest request = AAIRequest.getRequestFromResource("vnf-image");
-                       request.addRequestProperty("vnf-image.att-uuid", att_uuid);
+                       request.addRequestProperty("vnf-image.vnf-image-uuid", vnf_image_uuid);
 
                        String rv = executor.get(request);
                        if(rv != null) {
@@ -2350,7 +2362,11 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                try {
                        for (ResultData datum : resultDataList) {
                                String data_type = datum.getResourceType();
-                               entity = new URL(datum.getResourceLink());
+                               String resourceLink = datum.getResourceLink();
+                               if(!resourceLink.isEmpty() && !resourceLink.toLowerCase().startsWith("http")) {
+                                       resourceLink = (new EchoRequest()).target_uri + resourceLink;
+                               }
+                               entity = new URL(resourceLink);
                        }
                } catch (Exception e) {
                        throw new AAIServiceException(e);
@@ -2368,6 +2384,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                        URL requestUrl = null;
 
                        String requestId = UUID.randomUUID().toString();
+                       StringBuilder errorStringBuilder = new StringBuilder();
 
                        try {
 
@@ -2449,16 +2466,14 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                        errorresponse.setRequestError(requestError);
                        throw new AAIServiceException(responseCode, errorresponse);
                    } else {
-//
-//                                     StringBuilder stringBuilder = new StringBuilder("\n");
-//                                     String line = null;
-//                                     while( ( line = reader.readLine() ) != null ) {
-//                                             stringBuilder.append("\n").append( line );
-//                                     }
-//                                     LOG.info(stringBuilder.toString());
-//
-//                     ErrorResponse errorresponse = mapper.readValue(stringBuilder.toString(), ErrorResponse.class);
-                       ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
+//                                     StringBuilder errorStringBuilder = new StringBuilder();
+                                       String line = null;
+                                       while( ( line = reader.readLine() ) != null ) {
+                                               errorStringBuilder.append("\n").append( line );
+                                       }
+
+                       ErrorResponse errorresponse = mapper.readValue(errorStringBuilder.toString(), ErrorResponse.class);
+//                     ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
                        LOGwriteEndingTrace(responseCode, responseMessage, mapper.writeValueAsString(errorresponse));
                        throw new AAIServiceException(responseCode, errorresponse);
                    }
@@ -2466,7 +2481,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                        } catch(AAIServiceException aaiexc) {
                                throw aaiexc;
                        } catch (Exception exc) {
-                               LOG.warn("GET", exc);
+                               LOG.warn(errorStringBuilder.toString(), exc);
                                throw new AAIServiceException(exc);
                        } finally {
                                if(inputStream != null){
@@ -2983,12 +2998,14 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                if(!key.contains(" = ")) {
                        if(isValidURL(key)) {
                                key = String.format("selflink = '%s'", key);
+                       } else if(isValidURI(key)) {
+                               key = String.format("resource-path = '%s'", key);
                        } else {
                                return response;
                        }
                }
 
-               HashMap<String, String> nameValues = keyToHashMap(key, ctx);
+               HashMap<String, String> nameValues = AAIServiceUtils.keyToHashMap(key, ctx);
 
                AAIRequest request = new SelfLinkRequest(type);
                if(nameValues.containsKey(PathRequest.RESOURCE_PATH.replaceAll("-", "_"))) {
@@ -3021,6 +3038,21 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                    return true;
                }
 
+       
+       public boolean isValidURI(String url) {
+
+               URI u = null;
+
+               try {
+                       u = new URI(url);
+               } catch (URISyntaxException e) {
+                       return false;
+               }
+
+               return true;
+       }
+
+       
        @Override
        protected boolean deleteRelationshipList(URL httpReqUrl, String json_text) throws AAIServiceException {
                if(httpReqUrl ==  null) {
@@ -3146,6 +3178,9 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                        break;
 
                default:
+                       if(key.contains("selflink =")) {
+                               break;
+                       }
                        if(!key.contains(String.format("%s.", normResource))) {
                                key = rewriteKey(resource, key, ctx);
                        }
@@ -3171,6 +3206,9 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                        break;
 
                default:
+                       if(key.contains("selflink =")) {
+                               break;
+                       }
                        if(!key.contains(String.format("%s.", normResource))) {
                                key = rewriteKey(resource, key, ctx);
                        }
@@ -3196,6 +3234,9 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                        break;
 
                default:
+                       if(key.contains("selflink =")) {
+                               break;
+                       }
                        if(!key.contains(String.format("%s.", normResource))) {
                                key = rewriteKey(resource, key, ctx);
                        }
@@ -3221,6 +3262,9 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                        break;
 
                default:
+                       if(key.contains("selflink =")) {
+                               break;
+                       }
                        if(!key.contains(String.format("%s.", normResource))) {
                                key = rewriteKey(resource, key, ctx);
                        }
@@ -3258,15 +3302,16 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                        fieldAnnotatedNames.add(primaryId);
                }
 
-               HashMap<String, String> nameValues = keyToHashMap(key, ctx);
+               HashMap<String, String> nameValues = AAIServiceUtils.keyToHashMap(key, ctx);
                Set<String> keyset = nameValues.keySet();
                for(String keyName : keyset) {
                        if(keyName.contains("."))
                                continue;
                        else {
                                String tmpKeyName = keyName.replaceAll("_", "-");
-                               if(fieldAnnotatedNames.contains(tmpKeyName)) {
-                                       key = key.replace(tmpKeyName, String.format("%s.%s", normResource, tmpKeyName));
+                               String valueToSubstitute = String.format("%s =", tmpKeyName);
+                               if(fieldAnnotatedNames.contains(tmpKeyName) && key.contains(valueToSubstitute)) {
+                                       key = key.replace(valueToSubstitute, String.format("%s.%s =", normResource, tmpKeyName));
                                }
                        }
                }
@@ -3275,4 +3320,13 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
                return key;
        }
 
+       @Override
+       public String getPathTemplateForResource(String resoourceName, String keys, SvcLogicContext ctx) throws MalformedURLException {
+               return AAIServiceUtils.getPathForResource(resoourceName, StringUtils.join(keys, " AND "), ctx);
+       }
+       
+       @Override
+       public boolean isDeprecatedFormat(String resource, HashMap<String, String> nameValues) {
+               return !AAIServiceUtils.isValidFormat(resource, nameValues);
+       }
 }