From a5ba6b59e4fc84b6aeae456c1860fcf7517e99cf Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Thu, 20 Feb 2020 09:45:11 +0200 Subject: Learn to get all model versions sorted from high to low Issue-ID: VID-771 Change-Id: I9c7cc3f319aa13a2dab33e8c584f7f7ed90cf847 Signed-off-by: Ittay Stern --- .../src/main/java/org/onap/vid/aai/AaiClient.java | 25 +++++++++++++++++----- .../java/org/onap/vid/aai/AaiClientInterface.java | 2 ++ 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'vid-app-common/src/main/java/org') diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/AaiClient.java b/vid-app-common/src/main/java/org/onap/vid/aai/AaiClient.java index 78164462c..e25fa8574 100644 --- a/vid-app-common/src/main/java/org/onap/vid/aai/AaiClient.java +++ b/vid-app-common/src/main/java/org/onap/vid/aai/AaiClient.java @@ -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; @@ -490,6 +491,15 @@ public class AaiClient implements AaiClientInterface { @Override public ModelVer getLatestVersionByInvariantId(String modelInvariantId) { + return maxModelVer(getAllVersionsByInvariantId(modelInvariantId)); + } + + @Override + public List getSortedVersionsByInvariantId(String modelInvariantId) { + return sortedModelVer(getAllVersionsByInvariantId(modelInvariantId)); + } + + private Stream getAllVersionsByInvariantId(String modelInvariantId) { if (modelInvariantId.isEmpty()) { throw new GenericUncheckedException("no invariant-id provided to getLatestVersionByInvariantId; request is rejected"); } @@ -497,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 aaiResponse = processAaiResponse(response, ModelVersions.class, null, VidObjectMapperType.FASTERXML); - Stream modelVerStream = toModelVerStream(aaiResponse.getT()); - return maxModelVer(modelVerStream); + return toModelVerStream(aaiResponse.getT()); } protected Stream 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) @@ -527,6 +533,15 @@ public class AaiClient implements AaiClientInterface { .orElseThrow(() -> new GenericUncheckedException("Could not find any version")); } + protected List sortedModelVer(Stream 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) { String depth = omitServiceInstances ? "1" : "2"; diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/AaiClientInterface.java b/vid-app-common/src/main/java/org/onap/vid/aai/AaiClientInterface.java index b4b908cc3..c322afa22 100644 --- a/vid-app-common/src/main/java/org/onap/vid/aai/AaiClientInterface.java +++ b/vid-app-common/src/main/java/org/onap/vid/aai/AaiClientInterface.java @@ -77,6 +77,8 @@ public interface AaiClientInterface extends ProbeInterface { ModelVer getLatestVersionByInvariantId(String modelInvariantId); + List getSortedVersionsByInvariantId(String modelInvariantId); + AaiResponse getServicesByProjectNames(List projectNames); AaiResponse getServiceModelsByDistributionStatus(); -- cgit 1.2.3-korg