From ab7d8152a6ec9f2641a857506eb6c34eba06ffa5 Mon Sep 17 00:00:00 2001 From: vasraz Date: Wed, 31 May 2023 16:13:43 +0100 Subject: Order response from external assets API Signed-off-by: Vasyl Razinkov Change-Id: Ia06c0b8bea06832f358420cac5c773d8d9a39a08 Issue-ID: SDC-4521 --- .../be/externalapi/servlet/AssetsDataServlet.java | 29 ++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/AssetsDataServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/AssetsDataServlet.java index e5b2a94584..3513065d7e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/AssetsDataServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/AssetsDataServlet.java @@ -50,7 +50,6 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; - import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic; @@ -69,6 +68,7 @@ import org.openecomp.sdc.be.datatypes.enums.FilterKeyEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.ecomp.converters.AssetMetadataConverter; import org.openecomp.sdc.be.externalapi.servlet.representation.AssetMetadata; +import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceAssetDetailedMetadata; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; import org.openecomp.sdc.be.model.Component; @@ -164,13 +164,13 @@ public class AssetsDataServlet extends AbstractValidationsServlet { value = "ETSI Version:3.3.1")}) @QueryParam("metadata") List metadata) throws IOException { ResponseFormat responseFormat; - String query = request.getQueryString(); - String requestURI = request.getRequestURI().endsWith("/") ? removeDuplicateSlashSeparator(request.getRequestURI()) : request.getRequestURI(); - String url = request.getMethod() + " " + requestURI; + final String query = request.getQueryString(); + final String requestURI = request.getRequestURI().endsWith("/") ? removeDuplicateSlashSeparator(request.getRequestURI()) : request.getRequestURI(); + final String url = request.getMethod() + " " + requestURI; log.debug("Start handle request of {}", url); - AuditingActionEnum auditingActionEnum = query == null ? AuditingActionEnum.GET_ASSET_LIST : AuditingActionEnum.GET_FILTERED_ASSET_LIST; - String resourceUrl = query == null ? requestURI : requestURI + "?" + query; - DistributionData distributionData = new DistributionData(instanceIdHeader, resourceUrl); + final AuditingActionEnum auditingActionEnum = query == null ? AuditingActionEnum.GET_ASSET_LIST : AuditingActionEnum.GET_FILTERED_ASSET_LIST; + final String resourceUrl = query == null ? requestURI : requestURI + "?" + query; + final DistributionData distributionData = new DistributionData(instanceIdHeader, resourceUrl); // Mandatory if (instanceIdHeader == null || instanceIdHeader.isEmpty()) { log.debug("getAssetList: Missing X-ECOMP-InstanceID header"); @@ -179,7 +179,7 @@ public class AssetsDataServlet extends AbstractValidationsServlet { return buildErrorResponse(responseFormat); } try { - Map filters = new EnumMap<>(FilterKeyEnum.class); + final Map filters = new EnumMap<>(FilterKeyEnum.class); if (category != null) { filters.put(FilterKeyEnum.CATEGORY, category); } @@ -213,7 +213,7 @@ public class AssetsDataServlet extends AbstractValidationsServlet { return buildErrorResponse(responseFormat); } else { log.debug("getAssetList: Asset Fetching Success"); - Either, ResponseFormat> resMetadata = assetMetadataConverter + final Either, ResponseFormat> resMetadata = assetMetadataConverter .convertToAssetMetadata(componentList.left().value(), requestURI, false, additionalMetadataKeysToInclude); if (resMetadata.isRight()) { log.debug("getAssetList: Asset conversion Failed"); @@ -221,7 +221,7 @@ public class AssetsDataServlet extends AbstractValidationsServlet { getComponentsUtils().auditExternalGetAssetList(responseFormat, auditingActionEnum, distributionData, requestId); return buildErrorResponse(responseFormat); } - Object result = RepresentationUtils.toRepresentation(resMetadata.left().value()); + final Object result = RepresentationUtils.toRepresentation(sortIfNeed(resMetadata.left().value(), additionalMetadataKeysToInclude)); responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK); getComponentsUtils().auditExternalGetAssetList(responseFormat, auditingActionEnum, distributionData, requestId); return buildOkResponse(responseFormat, result); @@ -384,4 +384,13 @@ public class AssetsDataServlet extends AbstractValidationsServlet { private String removeDuplicateSlashSeparator(String requestUri) { return requestUri.substring(0, requestUri.length() - 1); } + + private List sortIfNeed(final List assetMetadataList, final List additionalMetadataKeysToInclude) { + if (additionalMetadataKeysToInclude.contains("creationDate")) { + assetMetadataList.sort((am1, am2) -> ((ServiceAssetDetailedMetadata) am1).getAdditionalRequestedMetadata().get("creationDate") + .compareTo(((ServiceAssetDetailedMetadata) am2).getAdditionalRequestedMetadata().get("creationDate"))); + } + return assetMetadataList; + } + } -- cgit 1.2.3-korg