Respect owning-entity-id when searching instances by Subscriber
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / AaiClient.java
index c43779d..be77e2b 100644 (file)
@@ -27,10 +27,15 @@ import static java.util.stream.Collectors.toMap;
 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
 import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
 import static org.apache.commons.lang3.StringUtils.isEmpty;
+import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import java.io.IOException;
+import java.io.Serializable;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URLEncoder;
@@ -44,10 +49,12 @@ import javax.inject.Inject;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Response;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.utils.URIBuilder;
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
@@ -281,6 +288,60 @@ public class AaiClient implements AaiClientInterface {
                 .collect(toMap(SimpleResult::getNodeType, SimpleResult::getProperties));
     }
 
+    @Override
+    public AaiResponse<AaiGetVnfResponse> getVnfsByParamsForChangeManagement(String subscriberId, String serviceType, @Nullable String nfRole,
+        @Nullable String cloudRegion) {
+        String payloadAsString = "";
+        ResponseWithRequestInfo response;
+        ImmutableMap<String, Serializable> payload = getMapForAAIQueryByParams(subscriberId, serviceType,
+            nfRole, cloudRegion);
+        try {
+            payloadAsString = JACKSON_OBJECT_MAPPER.writeValueAsString(payload);
+        } catch (JsonProcessingException e) {
+            logger.error(e.getMessage());
+            ExceptionUtils.rethrow(e);
+        }
+        response = doAaiPut(QUERY_FORMAT_SIMPLE, payloadAsString, false, false);
+        AaiResponseWithRequestInfo<AaiGetVnfResponse> aaiResponse = processAaiResponse(response, AaiGetVnfResponse.class, false);
+        verifyAaiResponseValidityOrThrowExc(aaiResponse, aaiResponse.getAaiResponse().getHttpCode());
+        return aaiResponse.getAaiResponse();
+    }
+
+    private ImmutableMap<String, Serializable> getMapForAAIQueryByParams(String subscriberId,
+        String serviceType, @Nullable String nfRole, @Nullable String cloudRegion) {
+        // in a case cloudRegion is null using query/vnfs-fromServiceInstance-filter,
+        // otherwise using query/vnfs-fromServiceInstance-filterByCloudRegion
+        if (nfRole != null){
+            if (cloudRegion != null){
+                return ImmutableMap.of(
+                    "start", ImmutableList
+                        .of("/business/customers/customer/" + encodePathSegment(subscriberId) + "/service-subscriptions/service-subscription/" + encodePathSegment(serviceType) + "/service-instances"),
+                    "query",  "query/vnfs-fromServiceInstance-filterByCloudRegion?nfRole=" + nfRole + "&cloudRegionID=" + cloudRegion + ""
+                );
+            }else {
+                return ImmutableMap.of(
+                    "start", ImmutableList
+                        .of("/business/customers/customer/" + encodePathSegment(subscriberId) + "/service-subscriptions/service-subscription/" + encodePathSegment(serviceType) + "/service-instances"),
+                    "query",  "query/vnfs-fromServiceInstance-filter?nfRole=" + nfRole + ""
+                );
+            }
+        }
+
+        if (cloudRegion != null){
+            return ImmutableMap.of(
+                "start", ImmutableList
+                    .of("/business/customers/customer/" + encodePathSegment(subscriberId) + "/service-subscriptions/service-subscription/" + encodePathSegment(serviceType) + "/service-instances"),
+                "query",  "query/vnfs-fromServiceInstance-filterByCloudRegion?cloudRegionID=" + cloudRegion + ""
+            );
+        }
+
+        return ImmutableMap.of(
+            "start", ImmutableList
+                .of("/business/customers/customer/" + encodePathSegment(subscriberId) + "/service-subscriptions/service-subscription/" + encodePathSegment(serviceType) + "/service-instances"),
+            "query", "query/vnfs-fromServiceInstance-filter"
+        );
+    }
+
     private boolean isResourceExistByStatusCode(ResponseWithRequestInfo responseWithRequestInfo) {
         // 200 - is found
         // 404 - resource not found
@@ -315,19 +376,20 @@ public class AaiClient implements AaiClientInterface {
         }
 
         final AaiResponseWithRequestInfo<T> aaiResponse = processAaiResponse(responseWithRequestInfo, clz, VidObjectMapperType.FASTERXML, true);
+        verifyAaiResponseValidityOrThrowExc(aaiResponse, responseWithRequestInfo.getResponse().getStatus());
+        return aaiResponse.getAaiResponse().getT();
+    }
 
+    private void verifyAaiResponseValidityOrThrowExc(AaiResponseWithRequestInfo aaiResponse, int httpCode) {
         if (aaiResponse.getAaiResponse().getHttpCode() > 399 || aaiResponse.getAaiResponse().getT() == null) {
             throw new ExceptionWithRequestInfo(aaiResponse.getHttpMethod(),
-                    aaiResponse.getRequestedUrl(),
-                    aaiResponse.getRawData(),
-                    responseWithRequestInfo.getResponse().getStatus(),
-                    new InvalidAAIResponseException(aaiResponse.getAaiResponse()));
+                aaiResponse.getRequestedUrl(),
+                aaiResponse.getRawData(),
+                httpCode,
+                new InvalidAAIResponseException(aaiResponse.getAaiResponse()));
         }
-
-        return aaiResponse.getAaiResponse().getT();
     }
 
-
     private String getUrlFromLIst(String url, String paramKey, List<String> params){
         int i = 0;
         for(String param: params){
@@ -397,7 +459,7 @@ public class AaiClient implements AaiClientInterface {
     }
 
     @Override
-    public AaiResponse getVNFData(String globalSubscriberId, String serviceType) {
+    public AaiResponse<AaiGetVnfResponse> getVNFData(String globalSubscriberId, String serviceType) {
         String payload = "{\"start\": [\"business/customers/customer/" + globalSubscriberId + SERVICE_SUBSCRIPTIONS_PATH + encodePathSegment(serviceType) +"/service-instances\"]," +
                 "\"query\": \"query/vnf-topology-fromServiceInstance\"}";
         Response resp = doAaiPut(QUERY_FORMAT_SIMPLE, payload, false);
@@ -466,9 +528,9 @@ public class AaiClient implements AaiClientInterface {
     }
 
     @Override
-    public AaiResponse getSubscriberData(String subscriberId, boolean omitServiceInstances) {
+    public AaiResponse<Services> getSubscriberData(String subscriberId, boolean omitServiceInstances) {
         String depth = omitServiceInstances ? "1" : "2";
-        AaiResponse subscriberDataResponse;
+        AaiResponse<Services> subscriberDataResponse;
         Response resp = doAaiGet(BUSINESS_CUSTOMERS_CUSTOMER + subscriberId + "?depth=" + depth, false);
         subscriberDataResponse = processAaiResponse(resp, Services.class, null);
         return subscriberDataResponse;
@@ -555,7 +617,7 @@ public class AaiClient implements AaiClientInterface {
         }
     }
 
-    private AaiResponse processAaiResponse(Response resp, Class classType, String responseBody) {
+    private <T> AaiResponse<T> processAaiResponse(Response resp, Class<? extends T> classType, String responseBody) {
         return processAaiResponse(resp, classType, responseBody, VidObjectMapperType.CODEHAUS);
     }