filter a&ai query to discard relationship list
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / AaiClient.java
index c82f548..00137b6 100644 (file)
@@ -44,6 +44,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import java.util.function.Function;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import javax.inject.Inject;
 import javax.ws.rs.WebApplicationException;
@@ -143,13 +144,13 @@ public class AaiClient implements AaiClientInterface {
     }
 
     @Override
-    public AaiResponse getServicesByOwningEntityId(List<String> owningEntityIds){
+    public AaiResponse<OwningEntityResponse> getServicesByOwningEntityId(List<String> owningEntityIds){
         Response resp = doAaiGet(getUrlFromLIst("business/owning-entities?", "owning-entity-id=", owningEntityIds), false);
         return processAaiResponse(resp, OwningEntityResponse.class, null);
     }
 
     @Override
-    public AaiResponse getServicesByProjectNames(List<String> projectNames){
+    public AaiResponse<ProjectResponse> getServicesByProjectNames(List<String> projectNames){
         Response resp = doAaiGet(getUrlFromLIst("business/projects?", "project-name=",  projectNames), false);
         return processAaiResponse(resp, ProjectResponse.class, null);
     }
@@ -289,7 +290,7 @@ public class AaiClient implements AaiClientInterface {
     }
 
     @Override
-    public AaiResponse getVnfsByParamsForChangeManagement(String subscriberId, String serviceType, @Nullable String nfRole,
+    public AaiResponse<AaiGetVnfResponse> getVnfsByParamsForChangeManagement(String subscriberId, String serviceType, @Nullable String nfRole,
         @Nullable String cloudRegion) {
         String payloadAsString = "";
         ResponseWithRequestInfo response;
@@ -302,19 +303,43 @@ public class AaiClient implements AaiClientInterface {
             ExceptionUtils.rethrow(e);
         }
         response = doAaiPut(QUERY_FORMAT_SIMPLE, payloadAsString, false, false);
-        AaiResponseWithRequestInfo aaiResponse = processAaiResponse(response, JsonNode.class, 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) {
-        String nfRoleParam = nfRole != null ? "?nfRole=" + nfRole : "";
-        String query = "query/vnfs-fromServiceInstance-filter" + nfRoleParam;
+        // 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/" + subscriberId + "/service-subscriptions/service-subscription/" + serviceType + "/service-instances"),
-            "query", query
+                .of("/business/customers/customer/" + encodePathSegment(subscriberId) + "/service-subscriptions/service-subscription/" + encodePathSegment(serviceType) + "/service-instances"),
+            "query", "query/vnfs-fromServiceInstance-filter"
         );
     }
 
@@ -435,7 +460,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,6 +491,15 @@ public class AaiClient implements AaiClientInterface {
 
     @Override
     public ModelVer getLatestVersionByInvariantId(String modelInvariantId) {
+        return maxModelVer(getAllVersionsByInvariantId(modelInvariantId));
+    }
+
+    @Override
+    public List<ModelVer> getSortedVersionsByInvariantId(String modelInvariantId) {
+        return sortedModelVer(getAllVersionsByInvariantId(modelInvariantId));
+    }
+
+    private Stream<ModelVer> getAllVersionsByInvariantId(String modelInvariantId) {
         if (modelInvariantId.isEmpty()) {
             throw new GenericUncheckedException("no invariant-id provided to getLatestVersionByInvariantId; request is rejected");
         }
@@ -473,17 +507,13 @@ public class AaiClient implements AaiClientInterface {
         Response response = doAaiPut("query?format=resource&depth=0",  "{\"start\": [\"service-design-and-creation/models/model/" + modelInvariantId + "\"],\"query\": \"query/serviceModels-byDistributionStatus?distributionStatus=DISTRIBUTION_COMPLETE_OK\"}",false);
         AaiResponse<ModelVersions> aaiResponse = processAaiResponse(response, ModelVersions.class, null, VidObjectMapperType.FASTERXML);
 
-        Stream<ModelVer> modelVerStream = toModelVerStream(aaiResponse.getT());
-        return maxModelVer(modelVerStream);
+        return toModelVerStream(aaiResponse.getT());
     }
 
     protected Stream<ModelVer> toModelVerStream(ModelVersions modelVersions) {
         if (modelVersions == null)
             return null;
 
-        if (modelVersions == null)
-            return null;
-
         return Stream.of(modelVersions)
                 .map(ModelVersions::getResults)
                 .flatMap(java.util.Collection::stream)
@@ -503,11 +533,24 @@ public class AaiClient implements AaiClientInterface {
                 .orElseThrow(() -> new GenericUncheckedException("Could not find any version"));
     }
 
+    protected List<ModelVer> sortedModelVer(Stream<ModelVer> modelVerStream) {
+        if (modelVerStream == null)
+            return emptyList();
+
+        return modelVerStream
+            .sorted(comparing(ModelVer::getModelVersion, comparing(DefaultArtifactVersion::new)).reversed())
+            .collect(Collectors.toList());
+    }
+
     @Override
-    public AaiResponse getSubscriberData(String subscriberId, boolean omitServiceInstances) {
+    public AaiResponse<Services> getSubscriberData(String subscriberId, boolean omitServiceInstances) {
         String depth = omitServiceInstances ? "1" : "2";
-        AaiResponse subscriberDataResponse;
-        Response resp = doAaiGet(BUSINESS_CUSTOMERS_CUSTOMER + subscriberId + "?depth=" + depth, false);
+        AaiResponse<Services> subscriberDataResponse;
+       String query = depth.equals("1") ?
+                BUSINESS_CUSTOMERS_CUSTOMER + subscriberId + "?depth=" + depth :
+                BUSINESS_CUSTOMERS_CUSTOMER + subscriberId + "?depth=" + depth +"&nodes-only";
+                               
+        Response resp = doAaiGet(query, false);
         subscriberDataResponse = processAaiResponse(resp, Services.class, null);
         return subscriberDataResponse;
     }
@@ -593,7 +636,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);
     }